Using Client-Side Task-Based Operations with WCFFacility in Castle.Windsor

偶尔善良 提交于 2019-12-06 11:04:34

The WCFFacility provides some extension methods that conform to the old async pattern. These can easily be converted to Tasks.

Try these extension methods:

public static class ClientExtensions
{
    public static async Task<TResult> CallAsync<TProxy, TResult>(this TProxy proxy, Func<TProxy, TResult> method)
    {
        return await Task.Factory.FromAsync(proxy.BeginWcfCall(method), ar => proxy.EndWcfCall<TResult>(ar));
    }

    public static async Task CallAsync<TProxy>(this TProxy proxy, Action<TProxy> method)
    {
        await Task.Factory.FromAsync(proxy.BeginWcfCall(method), ar => proxy.EndWcfCall(ar));
    }
}

In an async method they can be called like this:

// Func<T>
var result = await client.CallAsync(p => p.SayThisNumber(42));

// Action
await client.CallAsync(p => p.DoSomething());
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!