JasperFx / lamar

Fast Inversion of Control Tool and Successor to StructureMap
https://jasperfx.github.io/lamar
MIT License
563 stars 118 forks source link

IServiceScopeFactory is being disposed #385

Closed cheng93 closed 10 months ago

cheng93 commented 11 months ago

https://learn.microsoft.com/en-us/dotnet/core/extensions/dependency-injection#scope-scenarios

Mentions that IServiceScopeFactory is a singleton. It also does not implement IDisposable, which means I assume throughout the lifetime of the application it should be available.

However I am finding that it is being disposed, probably due to Container also implementing IDisposable, and is causing an issue.

https://github.com/cheng93/ServiceScopeFactoryDisposed is a sample

calling http://localhost:5153/test will trigger it

cheng93 commented 11 months ago

I believe IServiceScopeFactory being injected into the service is not the root scope, but rather the request scope, thus it's being disposed.

https://github.com/JasperFx/lamar/blob/d5db3ac61f57a2fee0ae3b132d4726617e78097c/src/Lamar/IoC/Resolvers/ScopeInstance.cs#L58

I think it's due to this.

The resolver compiles down is equivalent to the following

scope => {
  var inline_serviceScopeFactory = (IServiceScopeFactory) scope // this should probably be scope.Root
  return new TestService(inline_serviceScopeFactory, ....)
}