Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
607 views
in Technique[技术] by (71.8m points)

wcf - Programmatically adding an endpoint

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...