【问题标题】:How to create a Dynamic Client Proxy Connection when the type is unknown?类型未知时如何创建动态客户端代理连接?
【发布时间】:2009-02-19 17:55:06
【问题描述】:

我知道我可以执行以下代码来在 WCF 中动态创建客户端端点连接:

BasicHttpBinding basic = 
    new BasicHttpBinding(BasicHttpSecurityMode.TransportCredentialOnly); 

basic.Security.Transport.ClientCredentialType = 
    HttpClientCredentialType.Ntlm; 

EndpointAddress serviceAddress = 
    new EndpointAddress("http://whatever/service.svc"); 

YourServiceClient m_client = new YourServiceClient(basic, serviceAddress);

问题是,在这种情况下,我需要知道“YourServiceClient”是什么。我想要做的是从数据库中获取类型“YourServiceClient”,并将其存储为对象。有谁知道我会怎么做这样的事情?我从数据库中检索到的对象中的“YourServiceClient”值在哪里?

【问题讨论】:

    标签: c# .net wcf


    【解决方案1】:

    你将无法做到这一点。基本上,您要求在运行时获取未知数,但在编译时绑定到已知类型。如果您尝试访问的服务具有某种共享接口,则根本无法做到这一点。

    如果它们确实具有相同的接口(意思是,相同的方法集等),那么您可以使用此处的示例在运行时创建自己的通道工厂,并获取实现服务接口的代理:

    http://msdn.microsoft.com/en-us/library/ms734681.aspx

    【讨论】:

      【解决方案2】:

      Nicholas Allen 在他的博客中介绍了(我认为)类似的内容,从 part 1 开始

      IIRC 还有一种能力,只接收原始 XML 消息,然后您可以自己处理,而不是使用特定类型的代理。

      【讨论】:

        【解决方案3】:

        如果有能够在这里使用泛型(如果没有,那么你可能不得不使用一些反射,如果没有)那么这就是你所追求的吗?

        public TContract GetService<TContract>(EndpointAddress address){
            var channelFactory = new ChannelFactory<TContract>(new NetTcpBinding(),address);
            return channelFactory.CreateChannel();
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-14
          • 1970-01-01
          • 1970-01-01
          • 2018-01-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多