PerWebRequest LifeStyle and Application_EndRequest

77 views
Skip to first unread message

Corey Coogan

unread,
Dec 23, 2009, 10:36:55 PM12/23/09
to Castle Project Users
Hi Gang,
I'm registering some components related to Linq2Sql using
PerWebRequest lifestyle. I see them get created, but they get
destroyed before my global's Application_EndRequest method gets
called. Is that by design? Does anyone know a work around? I want
to call commit on my UnitOfWork object to submitchanges() at the end
of every request.

Thanks,
Corey

Krzysztof Koźmic

unread,
Dec 24, 2009, 5:14:19 AM12/24/09
to castle-pro...@googlegroups.com
Can you post a sample solution with reproduction of the problem?
Which version of Windsor are you using?

> --
>
> You received this message because you are subscribed to the Google Groups "Castle Project Users" group.
> To post to this group, send email to castle-pro...@googlegroups.com.
> To unsubscribe from this group, send email to castle-project-u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/castle-project-users?hl=en.
>
>
>

Corey Coogan

unread,
Dec 24, 2009, 8:49:39 AM12/24/09
to Castle Project Users
I'm using 2.0.

Here's how I'm registering my stuff with PerWebRequest. I am creating
a DataCOntextProvider object that holds onto a L2S DataContext. That
object is injected into the UoW.

/// <summary>
/// Register the IUnitOfWorkManager to resolve to
LinqToSqlUnitOfWorkManager per web request
/// </summary>
public void RegisterLinq2SqlUnitOfWorkPerWebRequest()
{
_container.Register(Component.For<IUnitOfWorkManager>()
.LifeStyle.PerWebRequest
.ImplementedBy<LinqToSqlUnitOfWorkManager>());
}

/// <summary>
/// Register the IDataContextProvider to resolve to
DataContextProvider per web request
/// </summary>
public void RegisterDataContextProviderPerWebRequest()
{
_container.Register(Component.For<IDataContextProvider>()
.LifeStyle.PerWebRequest
.ImplementedBy<DataContextProvider>());
}


Now I am simply trying to pull the UoW from the container via the
CommonServiceLocator (both CSL and Windsor Adapter are 1.0) from the
EndRequest like this:

protected void Application_EndRequest(object sender, EventArgs e)
{
//ignore unless this is a page (.aspx) or handler (.ashx)
if (!RequestCanHaveContext())
return;

//get the IUnitOfWork manager
var uow =
ServiceLocator.Current.GetInstance<IUnitOfWorkManager>();

//if we have one, commit changes at the end of the request
if (uow != null)
{
//don't explicitly dispose of uow or we'll get
Disposed exceptions on the context
uow.Commit();
}

}


Thanks for your help,
cc

On Dec 24, 4:14 am, Krzysztof Koźmic <krzysztof.koz...@gmail.com>
wrote:

Mauricio Scheffer

unread,
Dec 26, 2009, 9:53:58 AM12/26/09
to Castle Project Users
Try moving your Application_EndRequest code to a httpmodule and
register it before the PerWebRequestLifestyleModule.

> On Dec 24, 4:14 am, Krzysztof Ko¼mic <krzysztof.koz...@gmail.com>

Reply all
Reply to author
Forward
0 new messages