Configuration & ASP.NET 2.0

Tuesday, July 5, 2005

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.


Comments
Bill Sunday, July 10, 2005
Hella Cool Scott!
dominick Saturday, August 6, 2005
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
scott Tuesday, August 9, 2005
Good point, Dominick
Martin Thursday, October 6, 2005
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
scott Thursday, October 6, 2005
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.

Greg Monday, November 21, 2005
It is possible to use this with ConnectionStrings in an app.config file for a Windows application?
scott Tuesday, November 22, 2005
Greg:

All the documentation I find says connectionStrings is used from ASP.NET only.
ross nelson Thursday, November 24, 2005
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
Joe Myrick Friday, December 16, 2005
Awesome! Thanks.
Leandro Koiti Saturday, February 4, 2006
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
MG Tuesday, February 28, 2006
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.
Bill Wednesday, March 8, 2006
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?
scott Wednesday, March 8, 2006
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!
David Tuesday, March 28, 2006
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>

Steve Thursday, April 13, 2006
Excellent! This is exactly what I was looking for. Thanks!
Pini Usha Sunday, July 23, 2006
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 ?!?!?
Andrew Saturday, October 14, 2006
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
Haricharan Merugula Tuesday, November 14, 2006
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?
gravatar rakesh Friday, April 16, 2010
hi scoot,i am developing an application in asp.net using c# and want to use an API but don't know how to use it could you tell me the exact procedure and code for it.
thanks
gravatar anita Tuesday, February 8, 2011
configuration
gravatar hemanth Tuesday, February 22, 2011
Its Very Good,

But I want To Know how To write api in asp.net & How To call Api in asp.net
Comments are now closed.
by K. Scott Allen K.Scott Allen
My Pluralsight Courses
The Podcast!