Add endpoint programmatically

I have a WCF service that I connect in a client application. I use the following in a 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 call the API for this service as follows:

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

Now I want to determine the endpoint programmatically. How can I do that? I commented on the section from the configuration file because I thought that I would need to put some code in the TestServiceClient instance to dynamically add the endpoint, but then it throws the next exception when the TestServiceClient instance is created.

Could not find the default endpoint element that references the contract "TestService.IService" in the client configuration of the ServiceModel section. Perhaps this is due to the fact that your application was not found in the configuration file, or because not a single endpoint element corresponding to this contract can be found in the client element.

How can i do this? Any point in the code examples for adding an endpoint will also be evaluated.

+3
wcf wcf-endpoint
source share
3 answers

To programmatically create endpoints and bindings, you can do this in a 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 can also define several endpoints in your service configuration and choose which one will dynamically connect at runtime.

In the client program, you will do the following:

 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(); 
+8
source share

Can you just use:

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

Please note that your bind setting will no longer apply, since you are not using this endpoint configuration in your configuration file. You will also have to override this.

+1
source share

You can try:

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

If you do not double-check the namespace in the TestService file correctly?

0
source share

All Articles