2

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

I've tried he solution entitled "Relocating app.config 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).

Community
  • 1
  • 1
Contango
  • 76,540
  • 58
  • 260
  • 305
  • p.s. The reason I need this is as follows: by default, WCF loads the app.config attached to the entry assembly, i.e. app.exe.config. However, as I'm distributing a .dll to a customer, I want to load my own configuration file without having to resort to overwriting the one attached to their executable. – Contango Nov 10 '10 at 00:43

3 Answers3

1

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.

Contango
  • 76,540
  • 58
  • 260
  • 305
0

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
  • Actually, there is two separate techniques to eliminate reliance on a fixed app.config, and you do not always have to copy settings from the DLL's app.config into the single config file. See the answers below. – Contango Nov 10 '10 at 13:24
0

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.

Contango
  • 76,540
  • 58
  • 260
  • 305