PerWebRequestLifeStyle won’t work from Application_Start

2,209 views
Skip to first unread message

Marnix

unread,
Sep 30, 2009, 12:00:44 PM9/30/09
to Castle Project Users
Hi,

If noticed that services with the PerWebRequestLifeStyle will not work
during initialization of the ASP.NET application. The problem is that
the PerWebRequestLifeStyle relies on an HttpModule which is not
initialized yet during the Application_Start event or at the time the
Application.Init method is invoked.

The result is a very descriptive (but incorrect) error message:

Looks like you forgot to register the http module
Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule. [..]

In addition to this the PerWebRequestLifeStyle breaks down during unit
testing outside ASP.NET with the same exception.

I realize that this lifestyle was not intended to be used outside of
requests but it would be nice if this was handled more gracefully.
I've blogged about the workaround I'm currently using which is to fall
back to TransientLifeStyle during application startup and when there
is no web context.
(see http://blog.alanta.nl/2009/07/castle-perwebrequestlifestyle-wont-work.html)

Does anybody know of a better way to handle the application-startup
and unit-testing scenario's with PerWebRequestLifeStyle?

Thanks,
Marnix

Mauricio Scheffer

unread,
Oct 1, 2009, 10:48:32 PM10/1/09
to Castle Project Users
I think the right way to handle this would be with a hybrid request-
transient lifestyle manager, similar to the one you posted... kinda
like activerecord's HybridWebThreadScopeInfo...

Would you create an enhancement request in the issue tracker?
http://support.castleproject.org/projects/IOC

Please link back to this thread.

Thanks

On Sep 30, 1:00 pm, Marnix <mar...@alanta.nl> wrote:
> Hi,
>
> If noticed that services with the PerWebRequestLifeStyle will not work
> during initialization of the ASP.NET application. The problem is that
> the PerWebRequestLifeStyle relies on an HttpModule which is not
> initialized yet during the Application_Start event or at the time the
> Application.Init method is invoked.
>
> The result is a very descriptive (but incorrect) error message:
>
> Looks like you forgot to register the http module
> Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule. [..]
>
> In addition to this the PerWebRequestLifeStyle breaks down during unit
> testing outside ASP.NET with the same exception.
>
> I realize that this lifestyle was not intended to be used outside of
> requests but it would be nice if this was handled more gracefully.
> I've blogged about the workaround I'm currently using which is to fall
> back to TransientLifeStyle during application startup and when there
> is no web context.
> (seehttp://blog.alanta.nl/2009/07/castle-perwebrequestlifestyle-wont-work...)

Marnix

unread,
Oct 7, 2009, 11:46:08 AM10/7/09
to Castle Project Users
I've entered the issue as a bug; an exception with the wrong error
message is generated. I'd say that's a bug.

http://support.castleproject.org/projects/IOC/issues/view/IOC-ISSUE-166

SimoneB

unread,
Oct 8, 2009, 5:46:54 AM10/8/09
to Castle Project Users
I think the problem is that you are trying to _resolve_ a component
with pwr lifestyle from the container at application startup. Why do
you need to do that? I am using the PWR lifestyle successfully since I
am resolving components not earlier than the BeginRequest step, when
the http modules are already initialized.

On Sep 30, 6:00 pm, Marnix <mar...@alanta.nl> wrote:
> Hi,
>
> If noticed that services with the PerWebRequestLifeStyle will not work
> during initialization of the ASP.NET application. The problem is that
> the PerWebRequestLifeStyle relies on an HttpModule which is not
> initialized yet during the Application_Start event or at the time the
> Application.Init method is invoked.
>
> The result is a very descriptive (but incorrect) error message:
>
> Looks like you forgot to register the http module
> Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule. [..]
>
> In addition to this the PerWebRequestLifeStyle breaks down during unit
> testing outside ASP.NET with the same exception.
>
> I realize that this lifestyle was not intended to be used outside of
> requests but it would be nice if this was handled more gracefully.
> I've blogged about the workaround I'm currently using which is to fall
> back to TransientLifeStyle during application startup and when there
> is no web context.
> (seehttp://blog.alanta.nl/2009/07/castle-perwebrequestlifestyle-wont-work...)

Mauricio Scheffer

unread,
Oct 8, 2009, 8:21:23 AM10/8/09
to Castle Project Users
I agree with you... as I was saying the only way to properly handle
this would be using a hybrid request-
transient lifestyle manager, so in fact in AppStart no HttpContext
would be used.
In fact, that's what Marnix does in his blog post.

Marvin Glenn Lacuna

unread,
May 24, 2016, 8:43:40 PM5/24/16
to Castle Project Users
Thanks to this discussion. I understand it clearer. I actually wrote a blog post that explains the issue in a code-level by decompiling the Castle.Windsor.dll. 

Fixed and Explained: Forgot to register the http module Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, here.

hammett

unread,
May 24, 2016, 10:38:23 PM5/24/16
to castle-pro...@googlegroups.com
No need to decompile it. The code is right here:
https://github.com/castleproject/Windsor/blob/master/src/Castle.Windsor/MicroKernel/Lifestyle/PerWebRequestLifestyleModule.cs
> --
> You received this message because you are subscribed to the Google Groups
> "Castle Project Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to castle-project-u...@googlegroups.com.
> To post to this group, send email to castle-pro...@googlegroups.com.
> Visit this group at https://groups.google.com/group/castle-project-users.
> For more options, visit https://groups.google.com/d/optout.



--
Cheers,
hammett
http://www.d-collab.com/
http://www.hammettblog.com/
Reply all
Reply to author
Forward
0 new messages