Get / change address from client endpoint configuration

I would like to save the endpoint configuration in a .config file, but be able to change 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 lives on a different port, a different path, and sometimes on a different binding configuration.

I want to be able to programmatically extract the address "net.tcp: // BASEURI / FooService /", replace "BASEURI" with the server address, and then pass this as the DuplexChannelFactory address 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));

, <baseAddress> , - , , URI .

. Proxy, DuplexChannelFactory.

+3
2

ChannelFactory, :

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);

, DuplexChannelFactory, .

+5

IEndpointBehavior URL- .

ServiceEndpoint ApplyClientBehavior:

void ApplyClientBehavior(
    ServiceEndpoint endpoint,
    ClientRuntime clientRuntime
)
{
    endpoint.Address = ...
}
0

Source: https://habr.com/ru/post/1794512/


All Articles