Skip to content

ObjectDisposedException in child lifetime scopes of lifetimeScope activated with configurationAction #1020

@BigApple1988

Description

@BigApple1988

I've reproduced a situation related to #971
Actually it's related indirectly to async code and unobserved tasks.
You can reproduce the error by writing simple test

        [Fact]
        public void NestedLifetimeScopesShouldNotBeDisposed()
        {
            var builder = new ContainerBuilder();
            builder.RegisterType<object>();
            var rootContainer = builder.Build();
            var parentLifetimeScope = rootContainer.BeginLifetimeScope(x => x.RegisterType<object>());
            var childLifetimeScope = parentLifetimeScope.BeginLifetimeScope();
            parentLifetimeScope.Dispose();
            var exception = Record.Exception(() => childLifetimeScope.Resolve<object>());
            Assert.Null(exception);
        }

I ran into this exception in my ASP.NET Web Api service with Owin middleware. There is a code, that begins new LifetimeScope and adds IOwinContext for each request by passing configurationAction to BeginLifetimeScope method
https://github.com/autofac/Autofac.Owin/blob/af5911b7163b301aa347adb45d5a703f8caa4ce0/src/Autofac.Integration.Owin/AutofacAppBuilderExtensions.cs#L390
So if you start lifetimeScope eg in your contoller and run a new Task in it, you have ObjectDisposedException in all nested lifetime scopes right after your "synchronous" code ends.

Code that throws exception was added in 0f01618
There is no pull request and I don't know if this behaviour is by design, but it worked in the past and there is no descriptions of this behaviour in any documentation (or I didn't find it).

I'll try to look and the code and propose a pull request if you say it's a bug

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions