Permalink
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (41 sloc) 5.61 KB
title ms.date dev_langs ms.assetid
How to: Call WCF Service Operations Asynchronously
03/30/2017
csharp
vb
0face17f-43ca-417b-9b33-737c0fc360df

How to: Call WCF Service Operations Asynchronously

This topic covers how a client can access a service operation asynchronously. The service in this topic implements the ICalculator interface. The client can call the operations on this interface asynchronously by using the event-driven asynchronous calling model. (For more information about the event-based asynchronous calling model, see Multithreaded Programming with the Event-based Asynchronous Pattern). For an example that shows how to implement an operation asynchronously in a service, see How to: Implement an Asynchronous Service Operation. For more information about synchronous and asynchronous operations, see Synchronous and Asynchronous Operations.

[!NOTE] The event-driven asynchronous calling model is not supported when using a xref:System.ServiceModel.ChannelFactory%601. For information about making asynchronous calls using the xref:System.ServiceModel.ChannelFactory%601, see How to: Call Operations Asynchronously Using a Channel Factory.

Procedure

To call WCF service operations asynchronously

  1. Run the ServiceModel Metadata Utility Tool (Svcutil.exe) tool with both the /async and the /tcv:Version35 command options together as shown in the following command.

    svcutil /n:http://Microsoft.ServiceModel.Samples,Microsoft.ServiceModel.Samples http://localhost:8000/servicemodelsamples/service/mex /a /tcv:Version35  
    

    This generates, in addition to the synchronous and standard delegate-based asynchronous operations, a WCF client class that contains:

    • Two <operationName>Async operations for use with the event-based asynchronous calling approach. For example:

      [!code-csharpEventAsync#1] [!code-vbEventAsync#1]

    • Operation completed events of the form <operationName>Completed for use with the event-based asynchronous calling approach. For example:

      [!code-csharpEventAsync#2] [!code-vbEventAsync#2]

    • xref:System.EventArgs?displayProperty=nameWithType types for each operation (of the form <operationName>CompletedEventArgs) for use with the event-based asynchronous calling approach. For example:

      [!code-csharpEventAsync#3] [!code-vbEventAsync#3]

  2. In the calling application, create a callback method to be called when the asynchronous operation is complete, as shown in the following sample code.

    [!code-csharpEventAsync#4] [!code-vbEventAsync#4]

  3. Prior to calling the operation, use a new generic xref:System.EventHandler%601?displayProperty=nameWithType of type <operationName>EventArgs to add the handler method (created in the preceding step) to the <operationName>Completed event. Then call the <operationName>Async method. For example:

    [!code-csharpEventAsync#5] [!code-vbEventAsync#5]

Example

[!NOTE] The design guidelines for the event-based asynchronous model state that if more than one value is returned, one value is returned as the Result property and the others are returned as properties on the xref:System.EventArgs object. One result of this is that if a client imports metadata using the event-based asynchronous command options and the operation returns more than one value, the default xref:System.EventArgs object returns one value as the Result property and the remainder are properties of the xref:System.EventArgs object.If you want to receive the message object as the Result property and have the returned values as properties on that object, use the /messageContract command option. This generates a signature that returns the response message as the Result property on the xref:System.EventArgs object. All internal return values are then properties of the response message object.

[!code-csharpEventAsync#6] [!code-vbEventAsync#6]

See Also

How to: Implement an Asynchronous Service Operation