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.
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.
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.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With