Begin Request fails with " Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed"

864 views
Skip to first unread message

pa...@pabich.eu

unread,
Jan 13, 2015, 7:01:09 PM1/13/15
to aut...@googlegroups.com
Hi,

I've been using Autofac in this particular MVC 5 app for over 2 years and everything has been fine. Yesterday I had a problem with the DB which resulted in a lot of SQL exceptions which in turn, this is my guess, managed to put app in a state where it always returns 500 as it can't even start processing requests. Details below. I've seen the is error before but that was at the end of the processing pipeline, not at the beginning. The only explanation I can come up with is that the root container got disposed so Autfac is not able to create child containers(lifetimescopes). Does this ring a bell ?

Thanks a lot

Pawel

Message:
"when trying to create a controller of type 'UserProfileController'. Make sure that the controller has a parameterless public constructor. ---> System.ObjectDisposedException: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed."

Stack Trace:

" at Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope(Object tag)
at Autofac.Integration.Mvc.RequestLifetimeScopeProvider.GetLifetimeScope(Action`1 configurationAction)
at Autofac.Integration.Mvc.AutofacDependencyResolver.GetService(Type serviceType)
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
--- End of inner exception stack trace ---
at System.Web.Mvc.DefaultControllerFactory.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType)
at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
"

pa...@pabich.eu

unread,
Jan 13, 2015, 10:15:22 PM1/13/15
to aut...@googlegroups.com, pa...@pabich.eu
I managed to reproduce it and it turns out that the root container (with tag "Root") is disposed. Is it possible for a child container to cause disposal of its parent ?

pa...@pabich.eu

unread,
Jan 13, 2015, 11:06:02 PM1/13/15
to aut...@googlegroups.com, pa...@pabich.eu
You've been an awesome rubber duck :). It turned out that the root container was shared between MVC and NSB. When NSB received a lot of DB errors it shut itself down and disposed the container in the process.

Alex Meyer-Gleaves

unread,
Jan 14, 2015, 1:41:52 AM1/14/15
to aut...@googlegroups.com, pa...@pabich.eu
Glad you found the problem. Sounds like NSB needs its own lifetime scope created off the root to stop it from taking the whole container down. I don't know how that's all wired together but it might be worth looking into.
Reply all
Reply to author
Forward
0 new messages