Configuration & ASP.NET 2.0

My latest article covers interesting features in the .NET 2.0 configuration API.

In 2.0 you can not only read from configuration files, but you can write to configuration files, too (even on remote machines).

There are permission issues to watch for, and of course any write to a web.config file will cause a web application to restart in a new AppDomain. There is some additional flexibility with both of these issues thanks to the new configSource attribute.

The configSource attribute allows you to move a configuration section to an external file. Combining this feature with restartOnExternalChanges=”false” means you can avoid application restarts when modifying application configurations. You can also set different ACLs on the external files, perhaps giving the ASP.NET worker process write permissions on a file with appSettings, but leaving write permissions to web.config in the hand of Administrators.

It’s also easy to encrypt entire sections of a configuration file, but alas, not the <processModel> section which may contain an account password. Read all the details here.

posted on Monday, July 04, 2005 11:47 PM by scott

Comments

Friday, July 08, 2005 1:24 PM by Anatoly Lubarsky: Weblog

# The Configuration API in .NET 2.0

Friday, July 08, 2005 1:25 PM by Anatoly Lubarsky: Weblog

# The Configuration API in .NET 2.0

Friday, July 08, 2005 1:26 PM by Anatoly Lubarsky: Weblog

# The Configuration API in .Net 2.0

Sunday, July 10, 2005 3:47 PM by Bill

# re: Configuration &amp;amp; ASP.NET 2.0

Hella Cool Scott!
Thursday, July 21, 2005 5:02 PM by Anatoly Lubarsky: Weblog

# The Configuration API in .Net 2.0

Thursday, July 21, 2005 5:07 PM by Anatoly Lubarsky: Weblog

# The Configuration API in .Net 2.0

Saturday, August 06, 2005 1:16 AM by dominick

# re: Configuration &amp;amp; ASP.NET 2.0

excellent information.

To make it even worse, a account that wants to modify web.config - does not only need write ACLs for this file - it also need create new files for the directory web.config resides in - that is because they create a temp file on the fly....

another point is that app domain restarts not only hurt performance, you also loose all inproc session state and the cache - which might be a even bigger issue.

but very good article.

dominick
Monday, August 08, 2005 6:50 PM by scott

# re: Configuration &amp; ASP.NET 2.0

Good point, Dominick
Thursday, October 06, 2005 1:27 PM by Martin

# re: Configuration &amp;amp; ASP.NET 2.0

Thanks for your great article(s).

Can the the <connectionStrings> section only be used in web applications or also in windows forms apps? Is the possibility to have this section encrypted the only advantage over having the connection strings in the <appSettings> section?

Martin
Thursday, October 06, 2005 2:41 PM by scott

# re: Configuration &amp; ASP.NET 2.0

Hi Martin:

I believe <connectionStrings> is an ASP.NET only thing. I believe there are advantages to moving the connection strings out of appSettings because the ASP.NET admin tools will be able to undertand exactly where your connection strings are. Thats one advantage.

Monday, November 21, 2005 12:53 PM by Greg

# re: Configuration &amp;amp; ASP.NET 2.0

It is possible to use this with ConnectionStrings in an app.config file for a Windows application?
Monday, November 21, 2005 9:19 PM by scott

# re: Configuration &amp; ASP.NET 2.0

Greg:

All the documentation I find says connectionStrings is used from ASP.NET only.
Thursday, November 24, 2005 2:27 PM by ross nelson

# re: Configuration &amp;amp; ASP.NET 2.0

Excellent article. Everything you really need to know in one spot and a good starter for going deeper.

Question: any idea if you can create a custom configuration manager from whatever the base class for web configuraiton manager is? Im about to have a look myself. Im architecting a large distributed project at the moment and am looking at having a configuration service which a multitude of web services will subscribe to. On startup in the application_start events they would start pulling their configs down from the service.

Theres no real reason this has to inherit from the existing class other than to give .net developers we bring in a nice standard known config class structure to deal with.

Cheers
Ross
Thursday, December 15, 2005 7:56 PM by Joe Myrick

# re: Configuration &amp;amp; ASP.NET 2.0

Awesome! Thanks.
Saturday, February 04, 2006 12:15 PM by Leandro Koiti

# re: Configuration &amp;amp; ASP.NET 2.0

Hello there,I was wondering if is there a way to use the configuration files just like in ASP.NET 1.1
e.g.: I have one web application containing a web.config file, a windows form application containing an app.config file and one class library a app.config file, all of them have the same keys, what I want is that the top level class, in this case, the class library, get the values from the requesting class, and not from its own...
on ASP.NET 1.1 I was able to create this scenario, but now on ASP.NET 2.0 I have no idea about how am I going to do that...

I need this because my class library works independent of its source, it has a configuration file because I need to get some values (like connection string) from somewhere... but the class library haven't any data simply because after the compilation there'll be NO configuration the the class library, then the configuration files from the web and windows application replace the 'lost' configuration file
Tuesday, February 28, 2006 12:32 PM by MG

# re: Configuration &amp;amp; ASP.NET 2.0

Hi Scott,

Thanks for the excellent information.
I was wondering if there is a neat way to handle multiple environment configuration using a single web.config in ASP.NET 2.0. Also I can have 2 levels of configuration, firstly environment specifc - Dev, QA, Staging, Production and the second is within an environment country specific.

Thanks.
Tuesday, March 07, 2006 7:26 PM by Bill

# re: Configuration &amp;amp; ASP.NET 2.0

Nice article on config files, got me on my way!

One question with regards to resource files, in asp.net 2.0 is it possible to make changes to resource files and not have to restart the application?
Wednesday, March 08, 2006 9:13 AM by scott

# re: Configuration &amp; ASP.NET 2.0

Wow, I seem to be missing a lot of comments.

Bill: I think changing a resource file would cause a re-compile, maybe not a restart. I'm not sure, sorry. Either way, I don't think there would be a way to avoid either a recompile/restart.

MG: I would use "Web Deployment Projects" that the ASP.NET team released. You'll find it easy with Google.

Leaondro: I'm not sure, sorry!
Tuesday, March 28, 2006 1:53 PM by David

# re: Configuration &amp;amp; ASP.NET 2.0

The configSource property is great for externalizing the config for a complete service, but I wish to provider configSource for individual providers within a service.

eg.

<sectionGroup name="commonConfiguration" type="Sg.Common.CommonConfigurationGroup">
<section name="containerLayoutService" type="Sg.Common.Provider.ContainerLayoutServiceSection, CommonCore" />
</sectionGroup>

<commonConfiguration>
<containerLayoutService defaultProvider="DefaultContainerLayoutProvider">
<providers>
<add name="DefaultContainerLayoutProvider" type="Sg.Common.Provider.DefaultContainerLayoutProvider" />
<add name="CloudsPanel" type="Wb.Web.Ctrl.CloudsPanelLayoutProvider" configSource="Location1OfConfigurationForThisParticularProvider" />
<add name="MoneytechPanel" type="Wb.Web.Ctrl.MoneytechPanelLayoutProvider" configSource="Location2OfConfigurationForThisParticularProvider"/>
<add name="WhityPanel" type="Wb.Web.Ctrl.WhityPanelLayoutProvider" configSource="Location3OfConfigurationForThisParticularProvider"/>
</providers>
</containerLayoutService>
</commonConfiguration>

I also would have thought that configSource should be available on a section element.

Eg.

<sectionGroup name="commonConfiguration" type="Sg.Common.CommonConfigurationGroup">
<section name="containerLayoutService" type="Sg.Common.Provider.ContainerLayoutServiceSection, CommonCore" />
</sectionGroup>

<containerLayoutService configSource='blah.config' >

Could change to

<sectionGroup name="commonConfiguration" type="Sg.Common.CommonConfigurationGroup">
<section name="containerLayoutService" type="Sg.Common.Provider.ContainerLayoutServiceSection, CommonCore" configSource='blah.config' />
</sectionGroup>

Thursday, April 13, 2006 9:37 AM by Steve

# re: Configuration &amp;amp; ASP.NET 2.0

Excellent! This is exactly what I was looking for. Thanks!
Tuesday, May 16, 2006 5:57 AM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of

colleagues.&amp;nbsp;...
Tuesday, May 16, 2006 6:02 AM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
Wednesday, May 17, 2006 4:40 PM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
Saturday, May 20, 2006 1:17 AM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
Saturday, May 27, 2006 3:36 AM by mattonsoftware.com

# .NET Resources

The following links to .NET resources have been collated over time with the assistance of colleagues.&amp;nbsp;...
Saturday, July 22, 2006 11:28 PM by Pini Usha

# re: Configuration &amp;amp; ASP.NET 2.0

I don't know why, but when I look at the app.config in VS 2005 I don't see it encrypt.
But if I open the file not from VS 2005 i see it encrypt .... very strange ?!?!?
Saturday, August 26, 2006 6:55 PM by bigybj

# The Configuration API in .NET 2.0

The Configuration API in .NET 2.0
Friday, October 13, 2006 5:05 PM by Andrew

# re: Configuration ASP.NET 2.0

Hi Scott, thanks for the detailed write up. I have a question. Can you take a look at this post? --> http://forums.asp.net/thread/1428453.aspx
Tuesday, November 14, 2006 8:45 AM by Haricharan Merugula

# config file in IIS Folder

awesome code. it worked great when application is in C drive but it is not when it is in IIS Folder. I changed the config file settings to write(IIS folder properties) still didn't work. can you shed some info on this?
Friday, February 01, 2008 7:06 PM by Mainz

# The Configuration API in .NET 2.0

The configuration API and associated command line tools offer flexibility and convenience in updating, encrypting, and managing configuration files. When modifying configuration files, think about application restart issues and how to ensure write permissions to the configuration files.