When using Castle Windsor in web applications you would want to register many of your components with a lifestyle that is associated with a single request. This is the purpose of the PerWebRequest lifestyle.

If you try that with ASP.NET MVC on IIS7, you are likely to receive the following error message:

Looks like you forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule
Add '<add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.MicroKernel" />' to the <httpModules> section on your web.config.

Unfortunately, following the instructions in the error message doesn't help. There's a discussion about this issue on the Castle Project Users forum, but the gist of it is that if you don't need to resolve components during application startup, this shouldn't be an issue, and indeed it isn't - it seems to be something else.

In my case I seem to have solved the problem by registering the HTTP module in configuration/system.webServer/modules instead of configuration/system.web/httpModules.

Although I haven't had the opportunity to dive into the technical details to understand why this works, this seems to solve the problem on both Windows Vista and Windows Server 2008.


Comments

It is because IIS 7 running under Integration Pipeline(the default, and prefered mode) requires all HttpHandlers and HttpModules to be registered under:

/configuration/system.webServer/modules

It ignores the other HttpHandler and HttpModules defined in system.web.

If you have a website running under IIS6, or you have downgraded your IIS7 to run in "Classic .NET" pipeline mode, that's when you use /configuration/system.web/httpModules - as it's the OLD way of adding them.

IIS7 will even take your old web.config from IIS6 and 'upgrade it' by adding in the system.webServer/modules and /handlers section, and will copy over the older definitions. That is, if you use the IIS Manager to upgrade it. ;)
2010-01-14 22:04 UTC
Thanks for the explanation :)
2010-01-14 22:23 UTC


Wish to comment?

You can add a comment to this post by sending me a pull request. Alternatively, you can discuss this post on Twitter or Google Plus, or somewhere else with a permalink. Ping me with the link, and I may add it as a comment.

Published

Tuesday, 17 November 2009 12:44:37 UTC

Tags



"Our team wholeheartedly endorses Mark. His expert service provides tremendous value."
Hire me!