Labels

Wednesday, April 11, 2007

Change in Web.config no longer requires a restart, hence no loss of Session. Cache ...

 

Earliar in .Net1.x whenever there is any change occurs in Web.config, it results into restart of ASPNet Worker Process [aspnet_wp.exe].  This in turn results in loss of Session State, Cache …..

 

But with a quick fix in .Net2.0, we can get rid of this and freely make any changes.

 

Note: Major of the changes are required in the ‘appSettings’ section. However changes can be done in any of the sections in Web.config.

 

Fix is as below:

·         Introduce External Config files. i.e. Instead of placing the attributes of a section in Web.config, place them in an External config file and place a reference to the external file in that section of Web.config.

 

Example:

 

o        By default below is found in Web.config:

 

<?xml version="1.0"?>

<configuration>

                  <appSettings>

                        <add key="message" value="Changed" />

</appSettings>

<system.web>

<trace enabled="false" requestLimit="100" />

</system.web>

</configuration>

 

o        Introduce the concept of External Configuration as below:

 

<?xml version="1.0"?>

<configuration>

                  <appSettings configSource="appSettings.config" />

                 

<system.web>

<trace configSource="trace.config" />

</system.web>

</configuration>

 

o        Below are the individual config files.

 

appSettings.config

 

<?xml version="1.0"?>

<appSettings>

  <add key="message" value="Hello" />

</appSettings>

 

Trace.config

 

<?xml version="1.0"?>

<trace enabled="false" requestLimit="100" />

 

·         Use ‘restartOnExternalChanges’ attribute. [New in .Net 2.0]

o        This attribute is found in machine.config

o        By default value of this attribute is ‘true’.

o        But for the ‘appSettings’ and ‘system.data.dataset’, the values is set to ‘false’ in machine.config.

 

e.g.

 

<section name="appSettings" type="System.Configuration.AppSettingsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false" requirePermission="false" />

 

o        For the mentioned example to work, lets us set it for ‘trace’ section also.

 

<section name="trace" type="System.Web.Configuration.TraceSection, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" restartOnExternalChanges="false"/>

 

 

 

Now make changes as below.  These changes will not result into restart and Session loss.

 

Code to change ‘appSettings’ section:

 

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

AppSettingsSection section = (AppSettingsSection)config.GetSection("appSettings");

KeyValueConfigurationCollection coll = section.Settings;

string str = coll["message"].Value.ToString();

section.Settings["message"].Value = "Changed";

config.Save(ConfigurationSaveMode.Modified);

 

 

Code to change ‘trace’ section.

 

Configuration config = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath);

TraceSection section = (TraceSection)config.GetSection("system.web/trace");

section.Enabled = !section.Enabled;

section.RequestLimit = section.RequestLimit - 5;

config.Save(ConfigurationSaveMode.Modified);

 

Note:

·         Use restartOnExternalChanges with some care, as some parameters can truly only take effect if the application restarts.

·         If you do set restartOnExternalChanges to false for a section, make sure not to cache the parameters for the section in our application, and always read values through the WebConfigurationManager.

 

 

Regards,

Arun Manglick…

DISCLAIMER ========== This e-mail may contain privileged and confidential information which is the property of Persistent Systems Pvt. Ltd. It is intended only for the use of the individual or entity to which it is addressed. If you are not the intended recipient, you are not authorized to read, retain, copy, print, distribute or use this message. If you have received this communication in error, please notify the sender and delete all copies of this message. Persistent Systems Pvt. Ltd. does not accept any liability for virus infected mails.

No comments:

Post a Comment