autofac / Autofac

An addictive .NET IoC container
https://autofac.org
MIT License
4.5k stars 837 forks source link

OWIN/ WebApi2 nested lifetimescope exception #795

Closed tynor88 closed 8 years ago

tynor88 commented 8 years ago

Hi

Having followed the WebApi2 + OWIN integration documentation, I am facing an exception I cannot figure out how to solve.

Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.

Stacktrace: [ObjectDisposedException: Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it has already been disposed.] Autofac.Core.Lifetime.LifetimeScope.CheckNotDisposed() +67 Autofac.Core.Lifetime.LifetimeScope.BeginLifetimeScope(Object tag, Action1 configurationAction) +30 Owin.<<RegisterAutofacLifetimeScopeInjector>b__0>d.MoveNext() +200 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13908768 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<RunApp>d__5.MoveNext() +203 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +13908768 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +61 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.<DoFinalWork>d__2.MoveNext() +193 Microsoft.Owin.Host.SystemWeb.IntegratedPipeline.StageAsyncResult.End(IAsyncResult ar) +96 System.Web.AsyncEventExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +363 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +137

I am not creating any new lifetime scopes myself. I've created a sample project which is similar to my current setup.

It's difficult for me to debug, as this exception only happends when the site is deployed to a Virtual Machine (Windows Server 2012) running IIS 8.5. The exception occurs randomly whenever I hit either the WebApi2 endpoint or the OwinMiddleware HelloWorld endpoint. I even tried the Remote Debugging tools, and whenever I remote debug, the OWIN / WebApi2 endpoints work just fine.

It seems like the issue is around line 35 of the code in Startup.cs class:

        public void Configuration(IAppBuilder app)
        {
            var config = new HttpConfiguration();
            IContainer container = Bootstrapper.Bootstrap;

            ILogger logger = container.Resolve<ILogger>();

            try
            {
                config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
                app.UseAutofacMiddleware(container);

                app.UseAutofacWebApi(config);
                app.UseWebApi(config);

                config.MapHttpAttributeRoutes();

                //This line seems to give me some problems - If i don't call Foo() here, everything is working normally.
                //Foo is just manipulating a configuration file - services injected into it are just Singleton's.
                container.Resolve<IService>().Foo();
            }
            catch (Exception ex)
            {
                logger.Error(ex, "Unhandled exception occured");
                throw;
            }
        }
`
tillig commented 8 years ago

This issue was moved to autofac/Autofac.WebApi#18