seesharper / LightInject

An ultra lightweight IoC container
http://www.lightinject.net
MIT License
619 stars 122 forks source link

System.InvalidOperationException: Attempt to create a scoped instance without a current scope #399

Open Balbhimpsl opened 6 years ago

Balbhimpsl commented 6 years ago

Hi, I am writing the integration test using Nunit and in business logic Microsoft.Practices.ServiceLocator being used. I initialised ServiceLocator as below ServiceLocator.SetLocatorProvider(() => new LightInjectServiceLocator(webApiContainer)); where webApiContainer is Lightinject 5.1.0 ServiceContainer. I am using in-memory Httpserver to run the controllers in httpContext. Controller runs asynchronously using async/await. As per documentation on git hub, I set the below property after enable web API on container and did not see any improvement. webApiContainer.ScopeManagerProvider = new PerLogicalCallContextScopeManagerProvider(); Specific test if i run alone, it is running fine .If I run it in Parallel with other test Iget following error.

Microsoft.Practices.ServiceLocation.ActivationException: Activation error occured while trying to get instance of type IUserProvider, key "" ---> System.InvalidOperationException: Attempt to create a scoped instance without a current scope. at EPPS.LightInject.PerScopeLifetime.GetInstance(Func1 createInstance, Scope scope) in D:\EM\EPPS\Feature\EPPS.LightInject\LightInject.cs:line 5514 at DynamicMethod(Object[] ) at EPPS.LightInject.ServiceContainer.GetInstance(Type serviceType) in D:\EM\EPPS\Feature\EPPS.LightInject\LightInject.cs:line 2794 at EPPS.LightInject.ServiceLocation.LightInjectServiceLocator.DoGetInstance(Type serviceType, String key) in D:\EM\EPPS\Feature\EPPS.LightInject\ServiceLocation\LightInject.ServiceLocation.cs:line 61 at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 49 --- End of inner exception stack trace --- at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance(Type serviceType, String key) in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 53 at Microsoft.Practices.ServiceLocation.ServiceLocatorImplBase.GetInstance[TService]() in c:\Home\Chris\Projects\CommonServiceLocator\main\Microsoft.Practices.ServiceLocation\ServiceLocatorImplBase.cs:line 90 at Elli.EPPS.Integration.Common.BusinessObjects.Loan.get_User() in D:\EM\EPPS\Feature\Elli.EPPS.Integration\Common\BusinessObjects\Loan.cs:line 142 at Elli.EPPS.Integration.Domain.LockRates.LockRatesDomain.<GeneratePriceGrid>d__22.MoveNext() in D:\EM\EPPS\Feature\Elli.EPPS.Integration\Domain\LockRates\LockRatesDomain.cs:line 328 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter.GetResult() at Elli.EPPS.Integration.Domain.LockRates.LockRatesDomain.<LockRate>d__20.MoveNext() in D:\EM\EPPS\Feature\Elli.EPPS.Integration\Domain\LockRates\LockRatesDomain.cs:line 251 at Elli.EPPS.Integration.Domain.LockRates.LockRatesDomain.<LockRate>d__20.MoveNext() in D:\EM\EPPS\Feature\Elli.EPPS.Integration\Domain\LockRates\LockRatesDomain.cs:line 271 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at Elli.EPPS.Integration.Domain.LockRates.LockRatesDomain.d19.MoveNext() in D:\EM\EPPS\Feature\Elli.EPPS.Integration\Domain\LockRates\LockRatesDomain.cs:line 149 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at Elli.EPPS.Service.Controllers.RatesController.d10.MoveNext() in D:\EM\EPPS\Feature\Elli.EPPS.Service\Controllers\RatesController.cs:line 90 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Threading.Tasks.TaskHelpersExtensions.d3`1.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ApiControllerActionInvoker.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Web.Http.Filters.ActionFilterAttribute.d5.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Filters.ActionFilterAttribute.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ActionFilterResult.d2.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.AuthenticationFilterResult.d0.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Web.Http.Controllers.ExceptionFilterResult.d__0.MoveNext()

seesharper commented 6 years ago

Create a minimal repro of this and put it up on GitHub and we will PR a solution there 👍

Balbhimpsl commented 6 years ago

Hi Bernhard, Thanks for the quick reply.As suggested, I have attached minimal repro. Steps to run (reproduce) Extract attached Zip. Open solution Build the solution Click "Run all" in Test explorer Open log file from C:\temp\DemoLog_{yyyy.MM.dd}.text. It will have the exception details.

LightInjectServiceLocatorIssueRepro.zip

Balbhimpsl commented 6 years ago

Hi Bernhard, Any updates on this. I am waiting for your response.

Thanks and regards Balbhim

Balbhimpsl commented 6 years ago

Hi Bernhard, Could you please update on this.

ZOXEXIVO commented 6 years ago

ASP.NET Core 2.0

I have QueryDispatcher and CommandDispatcher registered as Transient. Each dispatcher contains injected Transient Repositories with scoped DbContexts.

In my middleware i access this dispatchers from context.RequestServices.GetService After success call of QueryDispatcher.Execute next request to CommandDispatcher breaks with:

System.InvalidOperationException: Attempt to create a scoped instance without a current scope