Async CTP - How can I use async/await to call a wcf service?

后端 未结 6 1956

If I call a WCF service method I would do something like this:

proxy.DoSomethingAsync();
proxy.DoSomethingAsyncCompleted += OnDoSomethingAsyncCompleted;
         


        
6条回答
  •  忘掉有多难
    2020-12-08 17:06

    It is quite common to have asynchronous clients calling a synchronous service.
    The following client and service contracts match (a bit magic used behind the scenes):

        [ServiceContract( Namespace="X", Name="TheContract" )]
        public interface IClientContractAsynchronous
        {
            [OperationContract]
            Task SendReceiveAsync( TRequest req );
        }
    
        [ServiceContract( Namespace="X", Name="TheContract" )]
        public interface IServiceContractSynchronous
        {
            [OperationContract]
            TResponse SendReceive( TRequest req );
        }
    

    The client interface is directly awaitable:

       var response = await client.Channel.SendReceiveAsync( request );
    

    It is not possible to use out or ref parameters in the operaton contract. All response data must be passed in the return value. This actually was a breaking change for me.
    I use this interface in AsyncWcfLib, it supports a Actor based programming model.

提交回复
热议问题