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
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
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
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.
All the documentation I find says connectionStrings is used from ASP.NET only.
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
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
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.
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?
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!
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>
But if I open the file not from VS 2005 i see it encrypt .... very strange ?!?!?
thanks
But I want To Know how To write api in asp.net & How To call Api in asp.net