开发者

WCF Client configuration - redirect multiple services to another host

开发者 https://www.devze.com 2023-03-25 12:04 出处:网络
Right now, i have multiple services in my client web.config like this: <endpoint address=\"http://hostname/ServiceA.svc\"

Right now, i have multiple services in my client web.config like this:

<endpoint address="http://hostname/ServiceA.svc"
         binding="wsHttpBinding" bindingConfiguration="WS开发者_如何学PythonHttpBinding_MyBinding"
         contract="ServiceReference.ISearchService" name="ServiceA">
</endpoint>

<endpoint address="http://hostname/ServiceB.svc"
         binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_MyBinding"
         contract="ServiceReference.ISearchService" name="ServiceB">
</endpoint>

and quite some more

This web.config makes switching between hosting environments difficult and error-prone, because you need to replace the "hostname" on all and each one of the endpoint addresses.

I want to somehow add an environment app setting or something somewhere that will let me specify a host, and have the endpoint be resolved like http://{hostA}/ServiceX.svc

Do i need to do some magic programmatically or can i do this from configuration alone?


You'll need to do it via code - there's no "wildcard" in config which you can use for the base address in the <system.serviceModel / client> section. You can, however, have some placeholder in config (like "localhost") and in code first load the config, then update the server name with the actual value (which can be stored in config as well, e.g., in the AppSettings, or somewhere else).

var factory = new ChannelFactory<ISearchService>("ServiceA");
factory.Endpoint.Address = ReplaceServerName(factory.Endpoint.Address);
var proxy = factory.CreateChannel();
...
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号