使用ChannelFactory和CreateChannel调用异步WCF

Mic*_*hal 8 .net c# wcf async-await

我处理的项目是Web服务器上托管的Web应用程序调用托管在应用服务器上的WCF服务.WCF调用的代理由ChannelFactory创建,调用通过channel进行,例如:

(省略使用块)

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

var users = channel.GetAllUsers();
Run Code Online (Sandbox Code Playgroud)

如果我理解的话,通过频道调用是异步的,Web服务器上的线程在请求​​期间是空闲的,只是等待响应.

我想像这样打电话异步:

var users = await channel.GetAllUsersAsync();
Run Code Online (Sandbox Code Playgroud)

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

有没有办法如何使用ChannelFactory调用方法异步?谢谢.

Leo*_*lev 7

您可以使用T4从原始界面自动生成包含异步版本方法的新界面,并在ChannelFactory 不更改服务器端接口的情况下使用它.

我使用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()#>?
Run Code Online (Sandbox Code Playgroud)

您将接口文件名传递给模板:

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

namespace MyProject
{
    [ServiceContract]
    interface IUserService
    {
        [OperationContract]
        List<User> GetAllUsers();
    }
}
Run Code Online (Sandbox Code Playgroud)

要获得新的:

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 ();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在您可以将它放在工厂中以异步方式使用通道:

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


Gaz*_*yer 6

不幸的是,没有.

从svcutil获取的异步方法是在代理中根据您的接口生成的.这样的原始WCF通道中没有任何内容.

唯一的方法是将服务引用更改为具有您不想要的本机异步调用,或者在通道周围创建自己的包装器并像生成的代理一样自己实现它们.


Ram*_*ein 5

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

有两种可能的解决方案:

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