2
votes

I (noob to light inject mvc) am using LightInject MVC in my asp.mvc 4 app with great results except an occasional exception. Now I am seeing it in pre-production. The exception is: System.InvalidOperationException: Attempt to create a scoped instance without a current scope.

My app start code is:

var container = new LightInject.ServiceContainer();
container.RegisterControllers();
container.RegisterAssembly(typeof(AppDDD.RegisterMe).Assembly, () => new PerScopeLifetime());
... scoped registrations
container.EnableMvc();

I get the error with a stack trace like:

System.InvalidOperationException: An error occurred when trying to create a controller of type 'MvcAPP.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor. ---> System.InvalidOperationException: Attempt to create a scoped instance without a current scope. at LightInject.PerScopeLifetime.GetInstance(Func1 createInstance, Scope scope) at DynamicMethod(Object[] ) at LightInject.ServiceContainer.<>c__DisplayClass40.<WrapAsFuncDelegate>b__3f() at LightInject.PerRequestLifeTime.GetInstance(Func1 createInstance, Scope scope) at DynamicMethod(Object[] ) at LightInject.ServiceContainer.TryGetInstance(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.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

I have created a couple of small test ASP MVC projects to isolate the exception with no success.

The Controllers are scoped per instance, and all the objects with in each instance are designated PerScopeLifetime.

Is there a tweak I can make, or should I stop using PerScopeLifetime? I must have scoped lifetimes for my EF contexts.

The exception is thrown before my controllers finish constructing, so it appears.

1
Hi! I am the author of LightInject and I would really like to help you out. There is a similar issue at GitHub that talks about the very same problem that you are experiencing. github.com/seesharper/LightInject/issues/49 I have not been able to reproduce this behavior yet and would really appreciate if you could come up with a sample project that causes the error. What version of LightInject are you using?seesharper

1 Answers

2
votes

I would like you to know that the issue has been resolved and getting the latest and greatest from NuGet should fix your problem.

Best regards

Bernhard Richter