Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
795 views
in Technique[技术] by (71.8m points)

configuration - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"]);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...