如何添加异步支持,为.NET 4.5 WCF服务,所以它不会破坏现有的客户端? [英] How to add async support to a .NET 4.5 WCF service so it doesn't disrupt existing clients?

查看:187
本文介绍了如何添加异步支持,为.NET 4.5 WCF服务,所以它不会破坏现有的客户端?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的WCF服务与SOAP端点,使用.NET 4.5。大多数现有的客户端code,使用的ChannelFactory< T> 代理方式

I have an existing WCF service with a SOAP endpoint, using .NET 4.5. Most of the existing client code is using the ChannelFactory<T> proxy approach.

我想改变服务支持异步 / 等待模型对各种服务器端我/ O和数据库操作。

I'd like to change the service to support the async / await model for various server-side I/O and database operations.

我遇到的问题是,添加异步关键字的WCF方法调用需要改变它们的接口签名任务&LT; T&GT; 。这反过来,似乎需要改变到客户端code

The problem I'm having is that adding the async keyword to the WCF method calls requires changing their interface signatures to Task<T>. That, in turn, seems to be requiring changes to the client code.

在保持服务code异步一路下来,有没有一种简单的方法,以保持公开的API不变?

While keeping the service code async "all the way down," is there a straightforward way to keep the exposed API unchanged?

推荐答案

只要你重命名你的服务器端的方法,包括字 XxxxxAsync 也不会改变客户方签字。

As long as you rename your server side method to include the word XxxxxAsync it will not change the clientside signature.

WCF全自动让两个端点的每一个方法,一个同步版本和异步版本。你可以看到这本与WCF测试客户端。

WCF automaticly makes two endpoints for every method, a synchronous version and a async version. You can see this this with the WCF test client.

例如,以下服务合同

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
}

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
}

当你火起来的WCF测试客户端,你会看到2种方法可

When you fire up the WCF test client you will see 2 methods available

如果我改变code以下

If I change the code to the following

[ServiceContract]
public interface IService1
{
    [OperationContract]
    Task<string> GetDataAsync(int value);
}

public class Service1 : IService1
{
    public async Task<string> GetDataAsync(int value)
    {
        await Task.Delay(value);
        return string.Format("You entered and awaited: {0}", value);
    }
}

我仍然可以从我的客户端调用同步字符串的GetData(INT)方法

请注意,您将无法使用相同的接口,客户方和服务器端再重新present的API(和你真的不应该,客户端界面应该有两个版本在里面。这样,客户端可以决定是否要进行阻塞调用者)。你将仍然能够然而使用它们之间共享模型

Note, you will not be able to use the same interface clientside and serverside anymore to represent the API (and you really shouldn't, the client side interface should have both versions in it. That way the client can decide if it wants to make a blocking call or not). You will still be able to use shared models between them however.

这篇关于如何添加异步支持,为.NET 4.5 WCF服务,所以它不会破坏现有的客户端?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆