question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ChildContainer disposing of TenantContext

See original GitHub issue

This 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:

  1. The middleware sets HttpContext.RequestServices to a nested container.
  2. 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.
  3. The middleware disposes of the nested container, which now triggers a dispose on the TenantContext.
  4. The TenantContext disposes of it’s properties, including the TenantContainer but it’s left in the memory cache
  5. Subsequent requests, the middleware resolves the TenantContext from the cache (the disposed instance) and get;s the disposed TenantContainer 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:open
  • Created 6 years ago
  • Comments:17 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
dazinatorcommented, Aug 25, 2017

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

1reaction
benfostercommented, Jul 16, 2017

@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 implements IDisposable 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:

        public virtual Task<IContainer> BuildAsync(TenantContext<TTenant> tenant)
        {
            Ensure.Argument.NotNull(tenant, nameof(tenant));

            var tenantContainer = Container.CreateChildContainer();
            tenantContainer.Configure(config => 
            {
                config.For<TenantContext<TTenant>>().Use(tenant);
                Configure(tenant.Tenant, config);
            });

            return Task.FromResult(tenantContainer);
        }

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 to csprojand 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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found