Open Closed

Blazor System.ObjectDisposedException #9699


User avatar
0
dhill created
  • Exception message and full stack trace:
  • Steps to reproduce the issue:

ABP 9.2.2 with Blazor Web App

We are getting random exceptions System.ObjectDisposedException when the system is under load.

======================================================== Dump Analysis for

Thread 6892 ExitCode E0434352 ExitCodeString CLR EXCEPTION DefaultHostName xxx.azurewebsites.net Managed Exception = System.ObjectDisposedException:Instances cannot be resolved and nested lifetimes cannot be created from this LifetimeScope as it (or one of its parent scopes) has already been disposed. CallStack - Managed Exception

 Autofac.Core.Lifetime.LifetimeScope.ThrowDisposedException()
 Blazorise.ComponentActivator.CreateInstance(System.Type)
 Microsoft.AspNetCore.Components.RenderTree.Renderer.InstantiateChildComponentOnFrame(Microsoft.AspNetCore.Components.RenderTree.RenderTreeFrame[], Int32, Int32)
 Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InitializeNewSubtree(DiffContext ByRef, Int32)
 Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.InsertNewFrame(DiffContext ByRef, Int32)
 Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext ByRef, Int32, Int32, Int32, Int32)
 Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(Microsoft.AspNetCore.Components.Rendering.RenderBatchBuilder, Microsoft.AspNetCore.Components.RenderFragment, System.Exception ByRef)
 Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 Microsoft.AspNetCore.Components.HtmlRendering.Infrastructure.StaticHtmlRenderer.HandleException(System.Exception)
 Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
 Microsoft.AspNetCore.Components.ComponentBase.StateHasChanged()
 Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext.&lt;InvokeAsync&gt;g__Execute|8_0(System.ValueTuple`3<System.Runtime.CompilerServices.AsyncTaskMethodBuilder,System.Action,Microsoft.AspNetCore.Components.Rendering.RendererSynchronizationContext>)
 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task, System.Threading.Tasks.ConfigureAwaitOptions)
 Volo.Abp.AspNetCore.Components.Web.LeptonXTheme.Components.ApplicationLayout.Common.ContentToolbar+<<OnInitializedAsync>b__7_1>d.MoveNext()
 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_1(System.Object)
 System.Threading.QueueUserWorkItemCallback.Execute()
 System.Threading.ThreadPoolWorkQueue.Dispatch()
 System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()

CallStack - Crashing Thread

 InlinedCallFrame
 InlinedCallFrame
 System.Runtime.EH.DispatchEx(System.Runtime.StackFrameIterator ByRef, ExInfo ByRef)
 System.Runtime.EH.RhThrowEx(System.Object, ExInfo ByRef)
 HelperMethodFrame
 System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
 System.Threading.Tasks.Task+<>c.<ThrowAsync>b__128_1(System.Object)
 System.Threading.QueueUserWorkItemCallback.Execute()
 System.Threading.ThreadPoolWorkQueue.Dispatch()
 System.Threading.PortableThreadPool+WorkerThread.WorkerThreadStart()
 DebuggerU2MCatchHandlerFrame

Native Call Stack

KERNELBASE!RaiseException+0x6c coreclr!SfiNext+0xea0fe coreclr!InlinedCallFrame::`vftable'

======================================================== Dump Analysis for

Thread 6892 ExitCode E0434352 ExitCodeString CLR EXCEPTION DefaultHostName xxx.azurewebsites.net CallStack - Crashing Thread


1 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • The System.ObjectDisposedException with Autofac's LifetimeScope in ABP Blazor applications typically occurs when a service or DbContext is accessed after its DI scope has been disposed. This is often seen under load or with improper async usage.

    Common Causes and Solutions:

    1. Async/Await Pitfalls:

      • If you start an async operation (e.g., in an event handler or background task) and do not properly await it, the DI scope may be disposed before the operation completes.
      • Always await async methods, especially in event handlers, background jobs, or distributed event handlers.
    2. Manual Service Scope Management:

      • If you manually create service scopes, ensure all operations using the scope complete before disposing it.
      • Do not wrap injected services in using statements; let the DI container manage their lifetimes.
    3. Unit of Work (UoW) Usage:

      • Do not disable the UnitOfWork pipeline unless you are managing it explicitly with IUnitOfWorkManager.
      • If you need a non-transactional UoW, use [UnitOfWork(isTransactional: false)] on your method.
    4. Blazor Component Lifetimes:

      • Avoid holding references to injected services or DbContexts in fields that outlive the component's lifetime.
      • Do not use services after the component is disposed.

    Example: Proper Async Usage in Event Handlers

    public async Task HandleEventAsync(MyEventData eventData)
    {
        // Always await async operations
        await _myService.DoSomethingAsync(eventData);
    }
    

    Example: Manual UnitOfWork Management

    using (var uow = _unitOfWorkManager.Begin(requiresNew: true, isTransactional: false))
    {
        // ... your logic here
        await uow.CompleteAsync();
    }
    

    Checklist:

    • Ensure all async methods are awaited.
    • Do not use disposed services or DbContexts.
    • Let DI manage service lifetimes.
    • Use UoW properly; do not disable unless you manage it manually.

    If the problem persists under load:

    • Review all background tasks, event handlers, and async code for proper scope and lifetime management.
    • Check for any static or long-lived references to scoped services.

    Sources:


    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.0.0-preview. Updated on September 01, 2025, 08:37