Mohsan
Mohsan

Reputation: 2509

C#: manage Multiple App.config files

I am facing a problem.

I have a dll which is interacting with a webservice and it saves its endpoint configuration in its app.config file.

I want to use this dll from my host application. The host application has its own config file. I have to merge the contents of dll's config to host's config each time when I change service endpoint.

Is there a way that I can use both config files. So the dll uses its own config whereas host application use its own config.

Upvotes: 10

Views: 10371

Answers (3)

Ken Egozi
Ken Egozi

Reputation: 1835

config files can include external files.

If you'd put the endpoint's config onto an external file, and then include it in yout host, you won't need to change the host's config every time

eg: in your app.config file:

...
<configSections>
  ...
  <section name="myEndpoint" type="System.Configuration.DictionarySectionHandler" />
  ...
</configSections>
...
<myEndpoint configSource="myEndpoint.config" />

then myEndpoint.config could look like that:

<?xml version="1.0" encoding="utf-8"?>
<myEndpoint>
  <add key="myKey" value="myValue" />
</myEndpoint>

and you can access the values from your code, similarly to reading normal app settings, like that:

var myEndpointConfig = (Hashtable)ConfigurationManager.GetSection("myEndpoint");
Console.WriteLine(myEndpointConfig["myKey"]);

Upvotes: 11

slugster
slugster

Reputation: 49984

This may help if you are using svcutil to generate your proxy: check the /config: and /mergeConfig switches, with those you can specify which config file receives the generated info and you can merge it in instead of just overwriting whatever is already there.

Upvotes: 0

Steven Sudit
Steven Sudit

Reputation: 19620

The right way is to merge the DLL's config file into the EXE's; that'll work out of the box. But see .NET DLL Settings and Config when there's a Web Reference - whats going on? for a way to explicitly open a config file and read it. The problem is that you'd need to FIND the config file first, and that's not necessarily easy. If the DLL is just sitting around, you could check the same directory. But if it's in the GAC, where do you look? I suggest sticking to the right way.

Upvotes: 2

Related Questions