Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 446
Mohsan Avatar asked Feb 04 '23 07:02

Mohsan


1 Answers

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"]);
like image 157
Ken Egozi Avatar answered Feb 05 '23 20:02

Ken Egozi