Common configuration files in .NET.

I have a solution that includes both a web application and a Windows NT application. These, of course, are two different projects, but within the same solution. However, they have a common configuration.

I currently have the same values ​​in both the web.config file and the app.config file. This is starting to get confusing, and I would like to have common configuration files for both applications as part of the solution.

  • Is there a problem for the Internet application if the configuration is not at the root level of the network expression? Are there any restrictions here?
  • I will lose caching and automatic network processing application if I do not use web.config
  • Generally, a bad idea to share a configuration as described?
+7
asp.net-mvc configuration windows-services
source share
2 answers

Well, you can β€œsqueeze” certain parts of the configuration into separate .config files and use them from both places.

eg. you can externalize the connection string settings as follows:

<connectionStrings configSource="connectionStrings.config" /> 

and then the file "connectionString.config":

 <?xml version="1.0" encoding="utf-8"?> <connectionStrings> <add name="ConfigurationDatabase" connectionString="server=.;Integrated Security=true;database=test"/> <add name="TestDatabase" connectionString="server=TEST;Integrated Security=true;database=test"/> </connectionStrings> 

In principle, any ConfigurationSection has such a parameter "configSource" that allows you to specify an external file to use.

This way you can share the common parts of the two configuration files.

Mark

+9
source share

You will still need web .config, as there are configuration elements that are web content that are not in the app.config of your service. As Mark says , using the ConfigSource attribute will allow you to share common elements.

Note that the appSettings element has a slight difference: the File attribute.

Specifies the relative path to an external file that contains user application configuration settings. The specified file contains the same settings that are specified in the appSettings attributes for adding, cleaning, and deleting and uses the same key / value pair format as these elements.

This behaves differently with respect to the ConfigSource attribute, because you do not need to replace the entire section with an external file, it can contain only the elements you want to add or override the values:

You can use the file attribute to specify a configuration file that provides additional parameters or overrides the parameters specified in the appSettings element.

If you use ConfigSource to exchange other elements, you will still have automatic application reloads when changing values ​​- the note about the restartOnExternalChanges attribute should be ignored for ASP.NET applications, however, using the File attribute means that the changes will not lead to a restart.

The contents of external files must be cached, so performance should not be affected.

+2
source share

All Articles