12.13 Create a One-Way Method with XML Web Services or Remoting


Problem

You want a Web method or remote component to perform a long task, and you don't want to force the client to wait while the code executes.

Solution

Create a one-way Web method by applying the SoapDocumentMethod or the SoapRpcMethod attribute and setting the attribute's OneWay property to true . Create a one-way Remoting method by applying the OneWay attribute from the System.Runtime.Remoting.Messaging namespace.

Discussion

With one-way methods , the client sends a request message, and the server responds immediately to indicate that the method began processing. This behavior has the following consequences:

  • The client doesn't need to wait while the server code executes.

  • The method can't return any information to the client, either through a return value or a ByRef parameter.

  • If the method throws an unhandled exception, it won't be propagated back to the client.

Clearly, one-way methods aren't suitable if the client needs to receive some information from the server. However, they are ideal for simply starting some sort of server-side task (for example, beginning a batch-processing job).

To create a one-way Web method, you need to apply a SoapDocumentMethod attribute (from the System.Web.Services.Protocols namespace) to the appropriate method and set the OneWay property to true . Here's an example XML Web service that provides two methods, each of which causes a 10-second delay. One of the two methods uses the OneWay attribute, so the client won't experience the 10-second wait.

 using System; using System.Web.Services; using System.Web.Services.Protocols; public class OneWayTestWebService {     [WebMethod()]     public void DoLongTaskWithWait() {         // (Start a long task and make the client wait.)         Delay(10);     }     [WebMethod, SoapDocumentMethod(OneWay=true)]     public void DoLongTaskWithoutWait() {         // (Start a long task but don't make the client wait.)         Delay(10);     }     private void Delay(int seconds) {         DateTime currentTime = DateTime.Now;         while (DateTime.Now.Subtract(currentTime).TotalSeconds < seconds) {}     } } 

This example assumes that your XML Web service and client are using SOAP document encoding (the default). If you're using remote procedure call (RPC) encoding, use the corresponding SoapRpcMethod attribute to mark a one-way method.

To create a one-way method in a component exposed over Remoting, you need to apply a OneWay attribute (from the System.Runtime.Remoting.Messaging namespace) to the appropriate method. The following code shows the same example with a remote component:

 using System; using System.Runtime.Remoting.Messaging; public class OneWayTestRemoting : MarshalByRefObject {     public void DoLongTaskWithWait() {         // (Start a long task and make the client wait.)         Delay(10);     }     [OneWay()]     public void DoLongTaskWithoutWait() {         // (Start a long task but don't make the client wait.)         Delay(10);     }     private void Delay(int seconds) {         DateTime currentTime = DateTime.Now;         while (DateTime.Now.Subtract(currentTime).TotalSeconds < seconds) {}     } } 
Note  

One-way methods aren't the only way to remove client delays. You can also modify the client to call any Web method asynchronously. In this case, the client will wait for the XML Web service to complete, but it will wait on another thread, so the client application can continue with other work. Asynchronous method calls are described in recipe 12.6.




C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net