How to call wcf service Asynchronously

前端 未结 2 1203
清酒与你
清酒与你 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: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 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);
    

提交回复
热议问题