Wednesday, 31 March 2010

One way to trigger a restart of your ASP.NET web application

Last week I built some code that dynamically published new URL rewrite rules into a file included into the web.config file of a .NET web app/site. It was done using classic ASP as it was part of a client's in-house CMS, and this part of the CMS enabled their end user to add their own friendly URLs to the site.

It became apparent early on that editing this included file automatically restarted the whole app, which was an undesirable situation given that customer sessions would be lost. My solution was to make use of the restartOnExternalChanges attribute of the section element in the web.config file that referred to the Intelligencia UrlRewriter:

<section name="rewriter" requirepermission="false" restartonexternalchanges="false" type="Intelligencia.UrlRewriter.Configuration.RewriterConfigurationSectionHandler, Intelligencia.UrlRewriter">
This meant that any edits to the included file wouldn't trigger a restart. But they also wouldn't be recognised until a restart. So then I had to give the user a way of restarting the app when they wanted to. So this time I used a similar entry in the web.config but with a contrary restartOnExternalChanges attribute setting:

<section name="rewriterRestarter" requirepermission="false" restartonexternalchanges="true" type="XmlConfigurator.XmlConfigurator, XmlConfigurator">
And later in the file:

<rewriterRestarter configsource="Config\urlrewriterRestarter.config">

With the right permissions set, the ASP CMS could make a simple edit to this new include and automatically trigger a restart.

Of course, we could have just edited the web.config directly to trigger a restart, but relaxing the permissions to achieve this just didn't seem right.

No comments:

Post a Comment

Comments are moderated, so you'll have to wait a little bit before they appear!