如何:使用通道工厂以异步方式调用操作

本主题介绍客户端如何在使用基于 ChannelFactory 的客户端应用程序时以异步方式访问服务操作。(当使用 System.ServiceModel.ClientBase 对象调用服务时,可以使用事件驱动的异步调用模型。有关更多信息,请参见如何:以异步方式调用 WCF 服务操作。有关基于事件的异步调用模型的更多信息,请参见Multithreaded Programming with the Event-based Asynchronous Pattern。)

本主题中的服务可实现 ICalculator 接口。客户端可以在此接口上以异步方式调用操作,这意味着像 Add 这样的操作将拆分为两个方法:BeginAddEndAdd。前一个方法启动调用,而后一个方法在操作完成时检索结果。有关演示如何在服务中以异步方式实现操作的示例,请参见如何:实现异步服务操作。有关同步和异步操作的详细信息,请参见同步和异步操作

过程

以异步方式调用 WCF 服务操作

  1. 按照下面的命令所示,使用 /async 选项运行 ServiceModel 元数据实用工具 (Svcutil.exe) 工具。

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

    这将生成该操作的服务协定的异步客户端版本。

  2. 创建一个异步操作完成时将要调用的回调函数,如下面的示例代码所示。

    Private Shared Sub AddCallback(ByVal ar As IAsyncResult)
        Dim result = (CType(ar.AsyncState, CalculatorClient)).EndAdd(ar)
        Console.WriteLine("Add Result: {0}", result)
    End Sub
    
    static void AddCallback(IAsyncResult ar)
    {
        double result = ((CalculatorClient)ar.AsyncState).EndAdd(ar);
        Console.WriteLine("Add Result: {0}", result);
    }
    
  3. 若要以异步方式访问服务操作,请创建客户端、调用 Begin[Operation](例如 BeginAdd),并指定一个回调函数,如下面的示例代码所示。

    Dim factory As New ChannelFactory(Of ICalculatorChannel)()
    Dim channelClient As ICalculatorChannel = factory.CreateChannel()
    
    ' BeginAdd
      Dim value1 = 100.0R
      Dim value2 = 15.99R
    
    Dim arAdd As IAsyncResult = channelClient.BeginAdd(value1, value2, AddressOf AddCallback, channelClient)
    Console.WriteLine("Add({0},{1})", value1, value2)
    
    ChannelFactory<ICalculatorChannel> factory = new ChannelFactory<ICalculatorChannel>();
    ICalculatorChannel channelClient = factory.CreateChannel();
    
    // BeginAdd
    double value1 = 100.00D;
    double value2 = 15.99D;
    
    IAsyncResult arAdd = channelClient.BeginAdd(value1, value2, AddCallback, channelClient);
    Console.WriteLine("Add({0},{1})", value1, value2);
    

    执行该回调函数时,客户端将调用 End<operation>(例如 EndAdd)以检索结果。

示例

上面过程中的客户端代码使用的服务可实现 ICalculator 接口,如下面的代码所示。在服务端,协定的 AddSubtract 操作由 Windows Communication Foundation (WCF) 运行库以同步方式调用,即使前面的客户端步骤是在客户端以异步方式调用的也是如此。在服务端,MultiplyDivide 操作用于在服务端以异步方式调用服务,即使客户端以同步方式调用这两个操作也是如此。此示例将 AsyncPattern 属性设置为 true。此属性设置与 .NET Framework 异步模式的实现一起,可让运行库以异步方式调用该操作。

<ServiceContract(Namespace := "http://Microsoft.ServiceModel.Samples")> _
Public Interface ICalculator
    <OperationContract> _
    Function Add(ByVal n1 As Double, ByVal n2 As Double) As Double

    <OperationContract> _
    Function Subtract(ByVal n1 As Double, ByVal n2 As Double) As Double

    'Multiply involves some file I/O so we'll make it Async.
    <OperationContract(AsyncPattern := True)> _
    Function BeginMultiply(ByVal n1 As Double, ByVal n2 As Double, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
    Function EndMultiply(ByVal ar As IAsyncResult) As Double

    'Divide involves some file I/O so we'll make it Async.
    <OperationContract(AsyncPattern := True)> _
    Function BeginDivide(ByVal n1 As Double, ByVal n2 As Double, ByVal callback As AsyncCallback, ByVal state As Object) As IAsyncResult
    Function EndDivide(ByVal ar As IAsyncResult) As Double
End Interface
[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
    [OperationContract]
    double Add(double n1, double n2);

    [OperationContract]
    double Subtract(double n1, double n2);

    //Multiply involves some file I/O so we'll make it Async.
    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginMultiply(double n1, double n2, AsyncCallback callback, object state);
    double EndMultiply(IAsyncResult ar);

    //Divide involves some file I/O so we'll make it Async.
    [OperationContract(AsyncPattern = true)]
    IAsyncResult BeginDivide(double n1, double n2, AsyncCallback callback, object state);
    double EndDivide(IAsyncResult ar);
}

另请参见

任务

异步