0

I have been setting up DI using LightInject for a new .NET Core 3.0 project. I followed the guide here: https://www.lightinject.net/microsoft.aspnetcore.hosting/. While running the project, I have encountered this error:

System.InvalidOperationException: Attempt to create a disposable instance without a current scope. at LightInject.PerRequestLifeTime.TrackInstance(Scope scope, IDisposable disposable) at LightInject.PerRequestLifeTime.GetInstance(Func`1 createInstance, Scope scope) at LightInject.ServiceContainer.TryGetInstance(Type serviceType) at LightInject.Microsoft.DependencyInjection.LightInjectServiceProvider.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.AspNetCore.Routing.Matching.DfaMatcherFactory.CreateMatcher(EndpointDataSource dataSource) at Microsoft.AspNetCore.Routing.EndpointRoutingMiddleware.InitializeCoreAsync()

Did anyone encounter the same issue and found a fix?

  • You're trying to resolve a service that implements IDisposable without having a "current scope", something that would later dispose of the service when you're done with it. You need to either configure LightInject to not bother, remove IDisposable from the service, or make sure you introduce a scope. – Lasse V. Karlsen Jul 01 '19 at 11:11
  • @lasse: No, the interface IFoo does not inherits from IDisposable. If you check the link above, I followed exactly like that. – Kurt Le Jul 02 '19 at 02:00
  • What about the concrete type that implements `IFoo`? – Lasse V. Karlsen Jul 02 '19 at 06:50
  • You need to post a [mcve]. – Lasse V. Karlsen Jul 02 '19 at 06:51

0 Answers0