Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I add WCF client endpoints programmatically?

Tags:

wcf

I need my service to consume other services, and I need to configure these dependencies in code. How do I do this?

This is very simple in config via 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'd be.

like image 493
Jeffrey Meyer Avatar asked May 25 '11 00:05

Jeffrey Meyer


People also ask

What is endpoint in WCF with example?

Endpoint behaviors achieve this by participating in the process of building a WCF runtime. An example of an endpoint behavior is the ListenUri property, which allows you to specify a different listening address than the SOAP or Web Services Description Language (WSDL) address.

Can WCF service have multiple endpoints?

As demonstrated in the Multiple Endpoints sample, a service can host multiple endpoints, each with different addresses and possibly also different bindings. This sample shows that it is possible to host multiple endpoints at the same address.


1 Answers

If you're using the Visual Studio generated proxy (via "Add Service Reference..."), then you're using the ClientBase abstract class & you'll have a number of constructors that allow you to pass in a config section, an endpoint, a binding etc.

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

And if you're instantiating a ChannelFactory then you again have a number of constructors to use.

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();
like image 193
Kirk Broadhurst Avatar answered Oct 02 '22 20:10

Kirk Broadhurst