我想将端点配置存储在 .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" />
每个服务都使用相同的契约 (ServiceContracts.MyService
),但存在于不同的端口、不同的路径,有时还使用不同的绑定(bind)配置。
我希望能够以编程方式提取地址“net.tcp://BASEURI/FooService/”,将“BASEURI”替换为服务器地址,然后在客户端连接时将其作为地址传递给 DuplexChannelFactory被 build 。例如:
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));
我知道客户端端点不支持服务端点的
注意:我没有使用 Proxy 类,而是直接使用 DuplexChannelFactory。
最佳答案
您可以在您的 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,但想法是一样的。
关于wcf - 从客户端端点配置获取/修改地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5108375/