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

ぐ巨炮叔叔 提交于 2019-12-08 00:29:32

问题


I would like to take advantage of the new task-based operations for a WCF client. I am currently using the WCFFacility as follows:

container.Register(Component
    .For<IAdminService>()
    .LifeStyle.Transient
    .AsWcfClient(new DefaultClientModel()
    {
        Endpoint = WCFHelpers.BasicHttp(settings.MaxReceivedMessageSize)
            .At(addr)
    }));

where IAdminService is the ServiceContract class. All the MSDN articles about task-based operations refer to setting a 'task based operations' tick box when importing the service reference. But in the style I am currently using, there is no imported service reference because I simple refer directly to the service contract interface.

So I am wondering how I can enable support for task-based operations with the least amount of changes to the current code.

[BTW - WCFHelpers is a utility class that generates a BindEndpointModel and addr is set up to an appropriate endpoint address prior to this code being executed]


回答1:


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());


来源:https://stackoverflow.com/questions/18302560/using-client-side-task-based-operations-with-wcffacility-in-castle-windsor

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