2014-05-09 62 views
6

我负责托管在Web服务器上的Web应用程序调用托管在应用服务器上的WCF服务的项目。代理对WCF呼叫通过的ChannelFactory创建和呼叫经由信道,例如由:如果我的理解以及调用经由信道是异步和线程在web服务器上与ChannelFactory和CreateChannel的异步WCF调用

(使用块省略)

var factory = new ChannelFactory<IUserService>(endpointConfigurationName); 
var channel = factory.CreateChannel(); 

var users = channel.GetAllUsers(); 

是在请求期间闲置并等待响应。

我想使异步调用像这样:

var users = await channel.GetAllUsersAsync(); 

有没有办法如何使的ChannelFactory和通道异步调用?我没有找到任何。我知道我可以通过svcutil/Add service reference生成异步方法,但我不想那样做。另外,我不想通过添加异步方法来更改应用程序服务器(IUserService)上的服务接口。

有什么办法如何与ChannelFactory调用异步方法?谢谢。

回答

3

不幸的是,没有。

您从svcutil获得的异步方法是基于您的接口在代理中生成的。原始的WCF频道没有这样的东西。

唯一的方法是将服务引用改为拥有您不想要的本机异步调用,或者创建您自己的封装器并自己像生成的代理一样实施它们。

+0

感谢您的回应。您有关于如何在频道周围创建自定义包装的任何提示/链接?我做了一些调查,但没有发现任何东西。 – Michal

4

不幸的是,这是不可能的,并有一个很好的理由。 CreateChannel返回实现提供的接口的对象(在您的示例中为IUserService)。这个接口不是异步感知的,所以它不可能用正确的方法返回一个对象。

有两种可能的解决方案:

  1. 创建您自己的代理,能够调用WCF服务。这意味着你需要编写自己的代理(或者让svcutil为你做)。
  2. 确保IUserService是一个返回任务的异步接口。这在WCF 4.5及更高版本中受支持。这是我经常使用的。主要的缺点是它使你的服务有点复杂,你需要调用异步方法(这也可能被认为是一种优势)。
6

您可以自动生成一个包含使用T4从原来的接口中的方法异步版本的新界面和ChannelFactorywithout changing interface on server side使用它。

我用NRefactory解析原始和产生新的C#源代码和AssemblyReferences.tt在T4模板中使用的NuGet包:

<#@ template debug="false" hostspecific="true" language="C#" #> 
<#@ include file="AssemblyReferences.tt" #> 
<#@ assembly name="System.Core" #> 
<#@ import namespace="System.Linq" #> 
<#@ import namespace="ICSharpCode.NRefactory.CSharp" #> 
<#@ output extension=".cs"#> 
<# 
var file = System.IO.File.ReadAllText(this.Host.ResolvePath("IUserService.cs")); 
if(!file.Contains("using System.Threading.Tasks;")) 
{ #> 
using System.Threading.Tasks; 
<# } #> 
<# 
CSharpParser parser = new CSharpParser(); 
var syntaxTree = parser.Parse(file); 


foreach (var namespaceDeclaration in syntaxTree.Descendants.OfType<NamespaceDeclaration>()) 
{ 
    namespaceDeclaration.Name += ".Client"; 
} 


foreach (var methodDeclaration in syntaxTree.Descendants.OfType<MethodDeclaration>()) 
{ 
    if (methodDeclaration.Name.Contains("Async")) 
     continue; 

    MethodDeclaration asyncMethod = methodDeclaration.Clone() as MethodDeclaration; 
    asyncMethod.Name += "Async"; 

    if (asyncMethod.ReturnType.ToString() == "void") 
     asyncMethod.ReturnType = new SimpleType("Task"); 
    else 
     asyncMethod.ReturnType = new SimpleType("Task", typeArguments: asyncMethod.ReturnType.Clone()); 

    methodDeclaration.Parent.AddChild(asyncMethod, Roles.TypeMemberRole); 
} 

#> 
<#=syntaxTree.ToString()#>​ 

你通过接口文件名模板:

using System.Collections.Generic; 
using System.ServiceModel; 

namespace MyProject 
{ 
    [ServiceContract] 
    interface IUserService 
    { 
     [OperationContract] 
     List<User> GetAllUsers(); 
    } 
} 

要获取新的:

using System.Threading.Tasks; 
using System.Collections.Generic; 
using System.ServiceModel; 

namespace MyProject.Client 
{ 
    [ServiceContract] 
    interface IUserService 
    { 
     [OperationContract] 
     List<User> GetAllUsers(); 

     [OperationContract] 
     Task<List<User>> GetAllUsersAsync(); 
    } 
} 

现在你可以把它n工厂使用渠道异步:

var factory = new ChannelFactory<MyProject.Client.IUserService>("*"); 
var channel = factory.CreateChannel(); 
var users = await channel.GetAllUsersAsync();