【问题标题】:Programmatically adding an endpoint以编程方式添加端点
【发布时间】:2012-07-19 23:56:46
【问题描述】:

我有一个在客户端应用程序中连接的 WCF 服务。我在配置文件中使用以下内容。

<system.serviceModel>  
    <bindings>  
      <basicHttpBinding>  
        <binding name="MyNameSpace.TestService" closeTimeout="00:01:00" openTimeout="00:01:00"  
            receiveTimeout="00:10:00" sendTimeout="00:01:00" allowCookies="false"  
            bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"  
            maxBufferSize="2147483647" maxBufferPoolSize="524288" maxReceivedMessageSize="2147483647"  
            messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"  
            useDefaultWebProxy="true">  
          <readerQuotas maxDepth="32" maxStringContentLength="2147483647" maxArrayLength="16384"  
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />  
          <security mode="None">  
            <transport clientCredentialType="None" proxyCredentialType="None"  
                realm="" />  
            <message clientCredentialType="UserName" algorithmSuite="Default" />  
          </security>  
        </binding>  
      </basicHttpBinding>  
    </bindings>  
    <client>  
      <endpoint address="http://localhost:9100/TestService" binding="basicHttpBinding"  
          bindingConfiguration="MyNameSpace.TestService" contract="TestService.IService" name="MyNameSpace.TestService" />  
    </client>  
</system.serviceModel>  

在代码中,我在这个服务上调用API如下,

TestServiceClient client = new TestServiceClient()
client.BlahBlah()

现在我想以编程方式定义端点。怎么可能呢?我从配置文件中注释掉了部分,因为我想我必须在 TestServiceClient 实例上放置一些代码来动态添加端点,但是它会在 TestServiceClient 实例化的地方引发以下异常。

找不到引用合约的默认端点元素 ServiceModel 客户端配置中的“TestService.IService” 部分。这可能是因为找不到配置文件 您的应用程序,或者因为没有与此匹配的端点元素 合同可以在客户端元素中找到。

我怎样才能做到这一点?此外,任何关于以编程方式添加端点的代码示例的点都将不胜感激。

【问题讨论】:

    标签: wcf wcf-endpoint


    【解决方案1】:

    要以编程方式创建端点和绑定,您可以在服务上执行此操作:

    ServiceHost _host = new ServiceHost(typeof(TestService), null);
    
    var _basicHttpBinding = new System.ServiceModel.basicHttpBinding();
                //Modify your bindings settings if you wish, for example timeout values
                _basicHttpBinding.OpenTimeout = new TimeSpan(4, 0, 0);
                _basicHttpBinding.CloseTimeout = new TimeSpan(4, 0, 0);
                _host.AddServiceEndpoint(_basicHttpBinding, "http://192.168.1.51/TestService.svc");
                _host.Open();
    

    您还可以在服务配置中定义多个端点,并选择在运行时动态连接到哪一个。

    在客户端程序上,您将执行以下操作:

    basicHttpBinding _binding = new basicHttpBinding();
    EndpointAddress _endpoint = new EndpointAddress(new Uri("http://192.168.1.51/TestService.svc"));
    
    TestServiceClient _client = new TestServiceClient(_binding, _endpoint);
    _client.BlahBlah();
    

    【讨论】:

      【解决方案2】:

      你能用吗:

      TestServiceClient client = new TestServiceClient();
      client.Endpoint.Address = new EndPointAddress("http://someurl");
      client.BlahBlah();
      

      请注意,您的绑定配置将不再适用,因为您没有在配置文件中使用该端点配置。你也必须覆盖它。

      【讨论】:

        【解决方案3】:

        你可以试试:

        TestServiceClient client = new TestServiceClient("MyNameSpace.TestService")
        client.BlahBlah()
        

        如果不重新检查文件TestService中的命名空间是否正确?

        【讨论】:

          猜你喜欢
          • 2014-01-03
          • 1970-01-01
          • 2016-01-28
          • 2013-02-19
          • 1970-01-01
          • 1970-01-01
          • 2012-02-29
          • 2021-08-23
          • 2018-04-10
          相关资源
          最近更新 更多