异步WCF客户端包装器

时间:2017-03-24 07:55:11

标签: c# wcf asynchronous

我根据What is the best workaround for the WCF client `using` block issue?

为WCF调用创建了自己的Wrapper
public delegate void UseServiceDelegate<T>(T proxy); 

public static class Service<T> 
{ 
    public static ChannelFactory<T> _channelFactory = new ChannelFactory<T>(""); 

    public static void Use(UseServiceDelegate<T> codeBlock) 
    { 
        IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel(); 
        bool success = false; 
        try 
        { 
            codeBlock((T)proxy); 
            proxy.Close(); 
            success = true; 
        } 
        finally 
        { 
            if (!success) 
            { 
                proxy.Abort(); 
            } 
        } 
     } 
} 

我可以用:

来调用它
Service<IOrderService>.Use(orderService => 
{ 
    orderService.PlaceOrder(request); 
}); 

如何为异步WCF调用创建它?

我已经尝试过的是:

public delegate Task UseAsyncServiceDelegate<T>(T proxy);

public static async Task UseAsync(UseAsyncServiceDelegate<T> codeBlock)
{
    IClientChannel proxy = (IClientChannel)_channelFactory.CreateChannel();
    bool success = false;
    try
    {
        await codeBlock((T)proxy);
        proxy.Close();
        success = true;
    }
    finally
    {
        if (!success)
        {
            proxy.Abort();
        }
    }
}

但它没有用。

0 个答案:

没有答案