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 = Som开发者_JS百科eFunctionThatGetsTheConfig(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.
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.
Implement an IEndpointBehavior and change the URL when being added.
You need to change the ServiceEndpoint
in ApplyClientBehavior
:
void ApplyClientBehavior(
ServiceEndpoint endpoint,
ClientRuntime clientRuntime
)
{
endpoint.Address = ...
}
精彩评论