AspNetDependencyInjection icon indicating copy to clipboard operation
AspNetDependencyInjection copied to clipboard

One-off "Cannot access a disposed object." during application startup

Open daiplusplus opened this issue 5 years ago • 3 comments

In my current project, when I press the Start Debugging button in VS2017 I sometimes get this exception. Reloading the page while the application is running or simply loading another URI in the application resolves the issue - so it isn't a showstopper or even an annoyance (as I rarely want to use the startup webpage anyway).

Type: System.Web.HttpUnhandledException
Message: Exception of type 'System.Web.HttpUnhandledException' was thrown.
TargetSite: Boolean HandleError(System.Exception)
HResult: -2147467259
Source: System.Web.
WebEventCode: 0
StackTrace:
   at System.Web.UI.Page.HandleError(Exception e)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
   at System.Web.UI.Page.ProcessRequest()
   at System.Web.UI.Page.ProcessRequest(HttpContext context)
   at ASP.default_aspx.ProcessRequest(HttpContext context)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.<>c__DisplayClass285_0.<ExecuteStepImpl>b__0()
   at System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step)
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

InnerException:

Type: System.ObjectDisposedException
Message: Cannot access a disposed object. Object name: 'IServiceProvider'.
Source: Microsoft.Extensions.DependencyInjection
StackTrace:
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ThrowHelper.ThrowObjectDisposedException()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.ServiceProviderEngine.GetService(Type serviceType, ServiceProviderEngineScope serviceProviderEngineScope)
   at Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)
   at lambda_method(Closure , IServiceProvider , Object[] )
   at AspNetDependencyInjection.ObjectFactoryCache.GetRequiredService(IServiceProvider serviceProvider, Type serviceType) in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\ObjectFactoryCache.cs:line 87
   at AspNetDependencyInjection.ObjectFactoryCache.GetRequiredService(Func`1 getServiceProvider, Type serviceType, Boolean useOverrides) in C:\git\me\Unity.WebForms\Unity.WebForms\AspNetDependencyInjection\AspNetDependencyInjection\ObjectFactoryCache.cs:line 55
   at __ASP.FastObjectFactory_myproject_merged.Create_ASP_page_master()
   at System.Web.UI.MasterPage.CreateMaster(TemplateControl owner, HttpContext context, VirtualPath masterPageFile, IDictionary contentTemplateCollection)
   at System.Web.UI.Page.get_Master()
   at System.Web.UI.Page.ApplyMasterPage()
   at System.Web.UI.Page.PerformPreInit()
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

daiplusplus avatar Nov 18 '19 07:11 daiplusplus

Encountered this issue today as well. I rebuilt the application and the problem was gone. But I'm curious of the reason :)

brz avatar Jun 01 '21 13:06 brz

I've added details in commit 7daeed9de745489fd0439233dba67f27e8b57eb7 to help see what's going on.

In short, something during processing of the first incoming HTTP request is disposing of the ServiceProviderEngine before the page is rendered.

daiplusplus avatar Aug 07 '21 00:08 daiplusplus

I got a good repro last-night from a production site that had a lot of users connected over SignalR.

It seems the problem is ASP.NET reuses the same IServiceProvider somehow for requests that come in before startup has fully-finished but after the init-only IServiceProvider was disposed. weird.

I've got log-files saved so I can take a look eventually.

daiplusplus avatar Dec 06 '21 12:12 daiplusplus