我从事的项目是,托管在 Web 服务器上的 Web 应用程序调用托管在应用程序服务器上的 WCF 服务。 WCF 调用的代理由 ChannelFactory 创建,调用通过 channel 进行,示例:
(省略 using block )
var factory = new ChannelFactory<IUserService>(endpointConfigurationName);
var channel = factory.CreateChannel();
var users = channel.GetAllUsers();
如果我理解得很好,通过 channel 调用是异步的,并且 Web 服务器上的线程在请求期间处于空闲状态,只需等待响应。
我想像这样进行异步调用:
var users = await channel.GetAllUsersAsync();
有没有办法调用 ChannelFactory 和异步 channel ?我没找到。我知道我可以通过 svcutil/添加服务引用生成异步方法,但我不想那样做。此外,我不想通过添加异步方法来更改应用服务器 (IUserService) 上的服务接口(interface)。
有什么方法可以调用与 ChannelFactory 异步的方法吗?谢谢。
最佳答案
您可以使用 T4 自动生成包含原始接口(interface)方法的异步版本的新接口(interface)并在 ChannelFactory
中使用它 without 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()#>
您将接口(interface)文件名传递给模板:
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 ();
}
}
现在你可以把它放在工厂中异步使用 channel :
var factory = new ChannelFactory<MyProject.Client.IUserService>("*");
var channel = factory.CreateChannel();
var users = await channel.GetAllUsersAsync();
关于c# - 使用 ChannelFactory 和 CreateChannel 进行异步 WCF 调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23565260/