CHAPTER 18 THE .NET REMOTING LAYER 603 the first step is to define a custom delegate to represent the remote method in question. At this point, the caller can make use of any of the techniques seen in Chapter 14 to invoke and receive the method return value. By way of a simple illustration, create a new console application (AsyncWKOCarProviderClient) and set a reference to the first iteration of the CarGeneralAsm.dll assembly. Now, update the Program class as so: class Program { // The delegate for the GetAllAutos() method. internal delegate List GetAllAutosDelegate(); static void Main(string[] args) { Console.WriteLine(”Client started! Hit enter to end”); RemotingConfiguration.Configure (”AsyncWKOCarProviderClient.exe.config”); // Make the car provider. CarProvider cp = new CarProvider(); // Make the delegate. GetAllAutosDelegate getCarsDel = new GetAllAutosDelegate(cp.GetAllAutos); // Call GetAllAutos() asynchronously. IAsyncResult ar = getCarsDel.BeginInvoke(null, null); // Simulate client-side activity. while(!ar.IsCompleted) { Console.WriteLine(”Client working…”); } // All done! Get return value from delegate. List allJBCs = getCarsDel.EndInvoke(ar); // Use all cars in List. foreach(JamesBondCar j in allJBCs) UseCar(j); Console.ReadLine(); } } Notice how the client application first declares a delegate that matches the signature of the GetAllAutos() method of the remote CarProvider type. When the delegate is created, you pass in the name of the method to call (GetAllAutos), as always. Next, you trigger the BeginInvoke() method, cache the resulting IAsyncResult interface, and simulate some work on the client side (recall that the IAsyncResult.IsCompleted property allows you to monitor if the associated method has completed processing). Finally, once the client s work has completed, you obtain the List<> returned from the CarProvider.GetAllAutos() method by invoking the EndInvoke() member, and pass each JamesBondCar into a static helper function named UseCar(): public static void UseCar(JamesBondCar j) { Console.WriteLine(”Can car fly? {0}”, j.isFlightWorthy); Console.WriteLine(”Can car swim? {0}”, j.isSeaWorthy); } Again, the beauty of the .NET delegate type is the fact that the logic used to invoke remote methods asynchronously is identical to the process of local method invocations.
Please visit Domain Name Hosting services for high quality webhost to host and run your jsp applications.
This entry was posted
on Friday, May 2nd, 2008 at 9:17 am and is filed under A1.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.