从客户端端点配置获取/修改地址

Chr*_*ham 3 wcf .net-3.5

我想将端点配置存储在.config文件中,但能够在运行时修改基址.EG:这些是我在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" />
Run Code Online (Sandbox Code Playgroud)

每个服务使用相同的合同(ServiceContracts.MyService),但生活在不同的端口,不同的路径,有时是不同的绑定配置.

我希望能够以编程方式提取地址"net.tcp:// BASEURI/FooService /",将"BASEURI"替换为服务器的地址,然后在创建客户端连接时将其作为地址传递给DuplexChannelFactory.例如:

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));
Run Code Online (Sandbox Code Playgroud)

我知道客户端端点不支持服务端点的<baseAddress>功能,但我的目的是以某种方式解决这个问题,以便我不必知道URI的其余部分或绑定是什么.

注意:我没有使用Proxy类,我直接使用DuplexChannelFactory.

Chr*_*son 5

您可以在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);
Run Code Online (Sandbox Code Playgroud)

好吧,你有DuplexChannelFactory,但想法是一样的.