【发布时间】:2011-04-26 22:31:44
【问题描述】:
我正在尝试创建一个通用的 WCF 服务调用程序实用程序,但我对泛型、委托和 lambda 的了解让我在最后的障碍中失败了。
我希望能够封装调用我的 WCF Web 服务的身份验证和期望处理,以便我可以仅使用接口、请求和响应类来使用 Web 服务。
我不明白如何传入我想要执行的方法名称 - 我尝试了 Func 路由,但由于我在下面实现的递归错误而感到困惑。我也不想走硬编码字符串/反射路线——我希望这是一个强类型的类。
请帮忙!
谢谢
public static TResponse Invoke<TService, TRequest, TResponse>(TRequest request, Func<TService, TRequest, TResponse> methodToExecute, string endpointConfigurationName) where TResponse : class
{
ChannelFactory<TService> channel = new ChannelFactory<TService>(endpointConfigurationName);
// attach auth credentials
channel.Credentials.UserName.UserName = "myUserName";
channel.Credentials.UserName.Password = "myPassword";
// create a proxy for the channel
TService proxy = channel.CreateChannel();
TResponse response;
try
{
response = methodToExecute(proxy, request);
channel.Close();
}
catch
{
// abort or close in a fail-safe manner
if (channel.State == CommunicationState.Faulted)
{
channel.Abort();
}
else
{
channel.Close();
}
throw;
}
return response;
}
【问题讨论】:
标签: .net wcf proxy proxy-classes