【问题标题】:Help creating a generic WCF service invoker - generics, delegates and lambda帮助创建通用 WCF 服务调用程序 - 泛型、委托和 lambda
【发布时间】: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


    【解决方案1】:

    这是我的尝试。我的版本没有请求类型作为参数。我展示了我希望你如何使用它(你没有展示你计划如何调用你的,但我怀疑问题出在调用上,而不是方法本身)。

        private class Response {}
    
        private interface IService
        {
            Response MyMethod(object i); 
        }
    
        public static void Foo()
        {
            object request = 1;
            Response response = Invoke((IService service) => service.MyMethod(request), "endpoint");
        }
    
        public static TResponse Invoke<TService, TResponse>(Func<TService, 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);
                channel.Close();
            }
            catch
            {
                // abort or close in a fail-safe manner
                if (channel.State == CommunicationState.Faulted)
                {
                    channel.Abort();
                }
                else
                {
                    channel.Close();
                }
    
                throw;
            }
    
            return response;
        }
    

    【讨论】:

      猜你喜欢
      • 2011-05-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-03
      • 2011-05-20
      相关资源
      最近更新 更多