How to call wcf service Asynchronously

前端 未结 2 1177
清酒与你
清酒与你 2020-11-30 13:44

if wcf service is design the below way then please guide me how call Add() function Asynchronously from client side. thanks

[Servic         


        
相关标签:
2条回答
  • 2020-11-30 14:05

    There is no such thing as an async wire communication pattern, and WFC is no exception. Going async across the wire requires fundamental changes in your service and your contract. Specifically your contract has to be a bidirectional contract and the 'end' must be pushed from the service to the client, along with the result. In other words, across the wire the 'async' becomes a callback.

    0 讨论(0)
  • 2020-11-30 14:11

    I think the best way is to convert the APM pattern into the Task pattern, using Task.Factory.FromAsync:

    public static class WcfExt
    {
        public static Task<int> AddAsync(this IAddTwoNumbers service, int a, int b)
        {
            return Task.Factory.FromAsync(
                 (asyncCallback, asyncState) =>
                     service.BeginAdd(a, b, asyncCallback, asyncState),
                 (asyncResult) =>
                     service.EndAdd(asyncResult), null);
        }
    }
    

    Usage:

    IAddTwoNumbers service = CreateWcfClientProxy();
    int result = await service.AddAsync(a, b);
    
    0 讨论(0)
提交回复
热议问题