Getting a System.ObjectDisposedException: The CancellationTokenSource has been disposed in .NET 8

Falanga, Rod, DOH 290 Reputation points
2025-08-06T21:52:54.0233333+00:00

I'm working on a new Blazor app, with server-side and WebAssembly projects in the VS solution. When I debug the app I get a, "Unhandled exception has occurred" message in the bottom of the page. Looking at my event viewer I see this, which suggests the problem is with the .NET Runtime:

Log Name:      Application
Source:        .NET Runtime
Date:          8/1/2025 9:51:35 AM
Event ID:      1026
Task Category: None
Level:         Error
Keywords:      Classic
User:          N/A
Computer:      MYPC.nmsg
Description:
Application: ServiceHub.Host.dotnet.x64.exe
CoreCLR Version: 8.0.1825.31117
.NET Version: 8.0.18
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException: The CancellationTokenSource has been disposed.
   at System.Threading.CancellationTokenSource.Cancel()
   at Microsoft.VisualStudio.Conversations.Service.Mcp.McpConfigurationService.Dispose()
   at Microsoft.VisualStudio.Conversations.Service.Mcp.McpManager.Dispose()
   at Microsoft.ServiceHub.HostStub.ServiceManager.TryDisposeObjectAsync(Object obj, TraceSource logger, String objectName)
   at Microsoft.ServiceHub.HostStub.ServiceManager.<>c__DisplayClass62_0.<<CreateDisposeServiceModuleTask>b__0>d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
Event Xml:
<Event xmlns="http://schemas.microsoft.com/win/2004/08/events/event">
  <System>
    <Provider Name=".NET Runtime" />
    <EventID Qualifiers="0">1026</EventID>
    <Version>0</Version>
    <Level>2</Level>
    <Task>0</Task>
    <Opcode>0</Opcode>
    <Keywords>0x80000000000000</Keywords>
    <TimeCreated SystemTime="2025-08-01T15:51:35.0794655Z" />
    <EventRecordID>59915</EventRecordID>
    <Correlation />
    <Execution ProcessID="16496" ThreadID="0" />
    <Channel>Application</Channel>
    <Computer>MYPC.nmsg</Computer>
    <Security />
  </System>
  <EventData>
    <Data>Application: ServiceHub.Host.dotnet.x64.exe
CoreCLR Version: 8.0.1825.31117
.NET Version: 8.0.18
Description: The process was terminated due to an unhandled exception.
Exception Info: System.ObjectDisposedException: The CancellationTokenSource has been disposed.
   at System.Threading.CancellationTokenSource.Cancel()
   at Microsoft.VisualStudio.Conversations.Service.Mcp.McpConfigurationService.Dispose()
   at Microsoft.VisualStudio.Conversations.Service.Mcp.McpManager.Dispose()
   at Microsoft.ServiceHub.HostStub.ServiceManager.TryDisposeObjectAsync(Object obj, TraceSource logger, String objectName)
   at Microsoft.ServiceHub.HostStub.ServiceManager.&lt;&gt;c__DisplayClass62_0.&lt;&lt;CreateDisposeServiceModuleTask&gt;b__0&gt;d.MoveNext()
--- End of stack trace from previous location ---
   at System.Threading.Tasks.Task.&lt;&gt;c.&lt;ThrowAsync&gt;b__128_1(Object state)
   at System.Threading.ThreadPoolWorkQueue.Dispatch()
   at System.Threading.PortableThreadPool.WorkerThread.WorkerThreadStart()
</Data>
  </EventData>
</Event>

I don't understand what's going on and I certainly don't understand why it is referring to .NET Version 8.0.18, when I am using .NET 9. I need help or direction as to how to resolve this issue.

Developer technologies | .NET | Blazor
{count} votes

1 answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 79,101 Reputation points Volunteer Moderator
    2025-08-07T19:01:56.1566667+00:00

    the error appears to an injected service instance that has already been disposed, when the service manager is doing cleanup. Your code is probably erroneously calling dispose on the service instance.

    in Blazor a scoped service lifetime is the the life of the circuit (app start to app end). so the same service instance is used for all pages and components for the circuit (Blazor app instance).

    if you need to call dispose on a page, the service instance should be a factory that creates instances, not the instance itself.

    looking closer, I see it was CancellationTokenSource that was disposed twice. as Blazor WASM does not support multi-threading, so a Task.Run() is synchronous, not sure why you need one.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.