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.

Print | posted @ Tuesday, July 05, 2005 3:47 AM

Comments on this entry:

Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Bill at 7/10/2005 10:47 PM

Hella Cool Scott!
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by dominick at 8/6/2005 8:16 AM

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
  
Gravatar # re: Configuration &amp;amp; ASP.NET 2.0
by scott at 8/9/2005 1:50 AM

Good point, Dominick
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Martin at 10/6/2005 8:27 PM

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
  
Gravatar # re: Configuration &amp;amp; ASP.NET 2.0
by scott at 10/6/2005 9:41 PM

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.

  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Greg at 11/21/2005 8:53 PM

It is possible to use this with ConnectionStrings in an app.config file for a Windows application?
  
Gravatar # re: Configuration &amp;amp; ASP.NET 2.0
by scott at 11/22/2005 5:19 AM

Greg:

All the documentation I find says connectionStrings is used from ASP.NET only.
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by ross nelson at 11/24/2005 10:27 PM

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
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Joe Myrick at 12/16/2005 3:56 AM

Awesome! Thanks.
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Leandro Koiti at 2/4/2006 8:15 PM

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
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by MG at 2/28/2006 8:32 PM

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.
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Bill at 3/8/2006 3:26 AM

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?
  
Gravatar # re: Configuration &amp;amp; ASP.NET 2.0
by scott at 3/8/2006 5:13 PM

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!
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by David at 3/28/2006 8:53 PM

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>

  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Steve at 4/13/2006 4:37 PM

Excellent! This is exactly what I was looking for. Thanks!
  
Gravatar # re: Configuration &amp;amp;amp; ASP.NET 2.0
by Pini Usha at 7/23/2006 6:28 AM

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 ?!?!?
  
Gravatar # re: Configuration ASP.NET 2.0
by Andrew at 10/14/2006 12:05 AM

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
  
Gravatar # config file in IIS Folder
by Haricharan Merugula at 11/14/2006 4:45 PM

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?
  

Your comment:

Title:
Name:
Email:
Website:
 
Italic Underline Blockquote Hyperlink
 
 
Please add 4 and 8 and type the answer here: