开发者

Relocating app.config to a custom path for WCF?

开发者 https://www.devze.com 2023-01-24 20:52 出处:网络
I need to load a custom app.config for WCF. I\'ve tried he solution entitled \"Relocating app.开发者_如何学Goconfig to a custom path\", but unfortunately, this technique just won\'t work for WCF, it

I need to load a custom app.config for WCF.

I've tried he solution entitled "Relocating app.开发者_如何学Goconfig to a custom path", but unfortunately, this technique just won't work for WCF, it still throws an error saying it can't find the .config file (correct me if I'm wrong).


Thanks to WCF Client without app.config/web.config by Kishore Gorjala, I eliminated all reliance on an app.config as follows:

EndpointAddress endpointAddress = new EndpointAddress("http://myServiceURL.com");
WSHttpBinding serviceBinding = new WSHttpBinding();
serviceBinding.ReceiveTimeout = new TimeSpan(0, 0, 120);
MyServiceClient myClient = new MyServiceClient(serviceBinding, endpointAddress);

According to this blog, you might want to try BasicHttpBinding instead of the WSHttpBinding as well.

This technique is also mentioned on the blog Minimal WCF server/client WITHOUT app.config.

Experimental evidence: This worked perfectly - and no more app.exe.config to worry about.


As you can see from the question you referenced, this does not work. In fact, it doesn't work anywhere in .NET - the problem is not specific to WCF.

There is only a single config file per AppDomain. Period. You will always have to copy settings from the DLL's app.config into the single config file.


There is a method to load app.config from a custom location, see Loading the WCF configuration from different files on the client side by Pablo M. Cibraro (aka Cibrax).

This method relies on overriding CustomClientChannel with a constructor that loads a custom app.config, then using the following code to create the client:

CustomClientChannel<ICalculator> channel = new CustomClientChannel<ICalculator>("OtherConfig.config");
ICalculator client = channel.CreateChannel();

Download the Microsoft Visual Studio sample project here.

To fix the compile errors under Visual Studio 2010, upgrade both projects to .NET 4.0, and reload the five referenced assemblies it can't find. To add it into your project is simple: just add the extra class, and replace the original channel instantiation line with the new one that uses a custom app.config.

0

精彩评论

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

关注公众号