Programmatically adding an endpoint

2019-01-20 13:45发布

问题:

I have a WCF service that I am connecting in client application. I am using following in configuration file.

<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>  

In the code, I am calling API on this service as follows,

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

Now I want to defined endpoint porgramatically. How can that be done? I commented out section from config file as I was thinking I will have to put some code on TestServiceClient instance to add endpoint dynamically but then it throws following exception at the point where TestServiceClient is instantiated.

Could not find default endpoint element that references contract 'TestService.IService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.

How can I accomplish this? Also any point on code examples for adding endpoint programmatically will be appreciated.

回答1:

To create endpoints and bindings programmatically, you could do this on the service:

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();

You could also define multiple endpoints in your service config, and choose which one to connect to dynamically at run time.

On the client program you would then do this:

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:

Can you just use:

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

Note that your binding configuration will no longer apply, as you're not using that endpoint configuration in your configuration file. You'll have to override that, too.



回答3:

You can try:

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

if not recheck namespace in file TestService is correct?