Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
816 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...