Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get/modify address from client Endpoint config

Tags:

.net-3.5

wcf

I'd like to store endpoint configurations in the .config file, but be able to modify the base address at runtime. EG: these are my endpoint definitions in app.config:

<endpoint address="net.tcp://BASEURI:1001/FooService/"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Common"
          contract="ServiceContracts.MyService"
          name="FooService" />

<endpoint address="net.tcp://BASEURI:1002/BarService/"
          binding="netTcpBinding" bindingConfiguration="NetTcpBinding_Special"
          contract="ServiceContracts.MyService"
          name="BarService" />

Each service uses the same contract (ServiceContracts.MyService), but live on a different port, different path, and sometimes a different binding configuration.

I want to be able to programmatically extract the address "net.tcp://BASEURI/FooService/", replace "BASEURI" with the address of the server, then pass this as the address to the DuplexChannelFactory when the client connection is created. EG:

string ServiceToUse = "FooService";

var endpointConfig = SomeFunctionThatGetsTheConfig(ServiceToUse);
string trueAddress = endpointConfig.Address.Replace("BASEURI", "192.168.0.1");
DuplexChannelFactory<FooService> client = 
    new DuplexChannelFactory<FooService>(ServiceToUse, new EndpointAddress(trueAddress));

I know that client endpoints don't support the <baseAddress> feature of Service endpoints, but my aim is to work-around that somehow so that I don't have to know what the rest of the URI or the binding is.

Note: I am not using a Proxy class, I'm using the DuplexChannelFactory directly.

like image 416
Chris Wenham Avatar asked Mar 19 '26 20:03

Chris Wenham


1 Answers

You can do this fairly easily on your ChannelFactory, e.g.:

ChannelFactory<IFoo> cf = new ChannelFactory<IFoo>("EndpointConfigName");
string address = cf.Endpoint.Address.Uri.ToString();
address = address.Replace("BASEURI", "192.168.0.1");
cf.Endpoint.Address = new EndpointAddress(address);

Well, you have DuplexChannelFactory, but the idea's the same.

like image 94
Chris Dickson Avatar answered Mar 22 '26 23:03

Chris Dickson