I'd like to programmatically modify my app.config file to set which service file endpoint should be used. What is the best way to do this at runtime? For reference:
<endpoint address="http://mydomain/MyService.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService" contract="ASRService.IASRService" name="WSHttpBinding_IASRService"> <identity> <dns value="localhost" /> </identity> </endpoint>
The endpoint address is represented in the Windows Communication Foundation (WCF) programming model by the EndpointAddress class, which contains an optional Identity property that enables the authentication of the endpoint by other endpoints that exchange messages with it, and a set of optional Headers properties, ...
The endpoint with the name "http" is used as the default endpoint for the service application.
Sometimes in our mind the question arise; can we implement multiple service contract in WCF service? And the answer is, Yes we can. Service class implement multiple service interfaces, and then expose each service using a different endpoint.
Now let us learn about each Endpoint. An address is the URL that defines where the WCF service is hosted.
Is this on the client side of things??
If so, you need to create an instance of WsHttpBinding, and an EndpointAddress, and then pass those two to the proxy client constructor that takes these two as parameters.
// using System.ServiceModel; WSHttpBinding binding = new WSHttpBinding(); EndpointAddress endpoint = new EndpointAddress(new Uri("http://localhost:9000/MyService")); MyServiceClient client = new MyServiceClient(binding, endpoint);
If it's on the server side of things, you'll need to programmatically create your own instance of ServiceHost, and add the appropriate service endpoints to it.
ServiceHost svcHost = new ServiceHost(typeof(MyService), null); svcHost.AddServiceEndpoint(typeof(IMyService), new WSHttpBinding(), "http://localhost:9000/MyService");
Of course you can have multiple of those service endpoints added to your service host. Once you're done, you need to open the service host by calling the .Open() method.
If you want to be able to dynamically - at runtime - pick which configuration to use, you could define multiple configurations, each with a unique name, and then call the appropriate constructor (for your service host, or your proxy client) with the configuration name you wish to use.
E.g. you could easily have:
<endpoint address="http://mydomain/MyService.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IASRService" contract="ASRService.IASRService" name="WSHttpBinding_IASRService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="https://mydomain/MyService2.svc" binding="wsHttpBinding" bindingConfiguration="SecureHttpBinding_IASRService" contract="ASRService.IASRService" name="SecureWSHttpBinding_IASRService"> <identity> <dns value="localhost" /> </identity> </endpoint> <endpoint address="net.tcp://mydomain/MyService3.svc" binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IASRService" contract="ASRService.IASRService" name="NetTcpBinding_IASRService"> <identity> <dns value="localhost" /> </identity> </endpoint>
(three different names, different parameters by specifying different bindingConfigurations) and then just pick the right one to instantiate your server (or client proxy).
But in both cases - server and client - you have to pick before actually creating the service host or the proxy client. Once created, these are immutable - you cannot tweak them once they're up and running.
Marc
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