ChildContainer disposing of TenantContext
See original GitHub issueThis bug can be seen with the sample if you make a few tweaks.
Problem
Take the structure map sample, then add a service or controller which has the AppTenant
injected into it’s constructor.
What seems to happen is this:
- The middleware sets
HttpContext.RequestServices
to a nested container. - After downstream middleware has been awaited (i.e and your service / controller has been resolved) the
TenantContext
appears to now be registered in the nested container. - The middleware disposes of the nested container, which now triggers a dispose on the
TenantContext
. - The
TenantContext
disposes of it’s properties, including theTenantContainer
but it’s left in the memory cache - Subsequent requests, the middleware resolves the
TenantContext
from the cache (the disposed instance) and get;s the disposedTenantContainer
which it attempts to use, resulting in an exception.
The reason you don’t see this problem in the current sample, is because AppTenant
is not injected anywhere. You also only see it from the second request onwards.
Issue Analytics
- State:
- Created 6 years ago
- Comments:17 (5 by maintainers)
Top Results From Across the Web
StructureMap Child Container - being disposed? · Issue #67
Middleware resolves tenant context --> the now disposed TenantContext is returned from memory cache. Middleware gets the TenantContainer from ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I believe Ben Foster was working on a new version with a fix. If you need something more urgent in the meantime you are welcome to try my saaskit- inspired library: https://github.com/dazinator/Dotnettency.Samples and https://github.com/dazinator/Dotnettency
@dazinator unfortunately this only fixes the problem resolving the tenant instance, not the TenantContext.
When not using the StructureMap package, registering the
TenantContext
per “scope” works since the scope is the request.For tenant containers the scope is the tenant context instance. However, since the
TenantContext
is resolved using the nested request container, and it implementsIDisposable
it will be disposed at the end of the request.One solution is to have the tenant container register the tenant context to itself. Nested containers will bubble up to the parent container if they can’t resolve a type and will only dispose transient dependencies. The code below effectively registers the
TenantContext
as a singleton in the tenant container:Unfortunately updating SaasKit with the above requires breaking changes to the
ITenantContainerBuilder
interface.An alternative solution is to drop
TenantContext
entirely. I’m going to complete the upgrade tocsproj
and clean things up a bit then will spike this out. I may do an interim release with the above fix if that’s what people would prefer.