How can I add WCF client endpoints programmatically?

I need my service to use other services, and I need to configure these dependencies in the code. How can I do it?

It is very easy to configure through the following (example):

<client> <endpoint name="registerService" address="http://127.0.0.1/registration/" binding="basicHttpBinding" contract="*"/> </client> 

But for some reason, finding the code equivalent is not as easy as I thought it would be.

+6
source share
1 answer

If you use the proxy created by Visual Studio (via "Add a link to the service ..."), you use the abstract ClientBase class and you will have several constructors that allow you to go through the configuration section, endpoint, binding, etc.

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

And if you create ChannelFactory, you again have several constructors.

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

 // create bindings & endpoints var binding = new System.ServiceModel.BasicHttpBinding(); var endpoint = new EndpointAddress("http://localhost/MyService.svc"); var factory = new ChannelFactory<IMyService>(binding, endpoint); var channel = factory.CreateChannel(); // then call your operations... channel.MyOperation(); 
+5
source

Source: https://habr.com/ru/post/888963/


All Articles