Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to programmatically modify WCF app.config endpoint address setting?

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> 
like image 557
alchemical Avatar asked Jun 08 '09 18:06

alchemical


People also ask

What is endpoint address in WCF?

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, ...

What is endpoint configuration name in WCF?

The endpoint with the name "http" is used as the default endpoint for the service application.

Can WCF service have multiple endpoints?

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.

Which of the following component in an endpoint specifies where the WCF service is hosted?

Now let us learn about each Endpoint. An address is the URL that defines where the WCF service is hosted.


1 Answers

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

like image 67
marc_s Avatar answered Sep 25 '22 14:09

marc_s