question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ObjectDisposedException when cancelling IHost.RunAsync()

See original GitHub issue

Stack Trace: System.ObjectDisposedException HResult=0x80131622 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.ServiceProviderEngineScope.GetService(Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Dapplo.Microsoft.Extensions.Hosting.Wpf.Internals.WpfThread.<PreUiThreadStart>b__1_0(Object s, ExitEventArgs e) in D:\a\1\s\src\Dapplo.Microsoft.Extensions.Hosting.Wpf\Internals\WpfThread.cs:line 46 at System.Windows.Application.OnExit(ExitEventArgs e) at System.Windows.Application.DoShutdown() at System.Windows.Application.ShutdownImpl() at System.Windows.Application.ShutdownCallback(Object arg) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at MS.Internal.CulturePreservingExecutionContext.CallbackWrapper(Object obj) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) — End of stack trace from previous location — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at MS.Internal.CulturePreservingExecutionContext.Run(CulturePreservingExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame) at System.Windows.Threading.Dispatcher.Run() at System.Windows.Application.RunDispatcher(Object ignore) at System.Windows.Application.RunInternal(Window window) at Dapplo.Microsoft.Extensions.Hosting.Wpf.Internals.WpfThread.UiThreadStart() in D:\a\1\s\src\Dapplo.Microsoft.Extensions.Hosting.Wpf\Internals\WpfThread.cs:line 90 at Dapplo.Microsoft.Extensions.Hosting.UiThread.BaseUiThread`1.InternalUiThreadStart() in D:\a\1\s\src\Dapplo.Microsoft.Extensions.Hosting.UiThread\BaseUiThread.cs:line 68 at System.Threading.ThreadHelper.ThreadStart_Context(Object state) at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) — End of stack trace from previous location — at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

This exception was originally thrown at this call stack: [External Code]

Code: public static async Task Main(string[] _) { CancellationTokenSource cancellationToken = new(TimeSpan.FromSeconds(20)); await host.RunAsync(cancellationToken.Token); }

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
Lakritzatorcommented, Mar 21, 2022

Fixed in 0.6.7 There is also a 1.0.3, which also has the fix, but it is a bit newer with updated dependencies.

0reactions
Lakritzatorcommented, Mar 21, 2022

I had a bit of a look through the code, and the issue is that the IServiceProvider is scoped to the ConfigureServices and not guaranteed to be available later on. It’s a bit documented in some issues, not very transparent. https://github.com/aspnet/Hosting/pull/1106

I’ve stored the IHostApplicationLifetime in the BaseUiThread. There is actually some code duplication, HandleApplicationExit should have been called.

Anyway, I will bring out a small fix for that on the current version, and afterwards update some more code & dependencies to make a 1.0 for now.

Just out of curiosity, can you tell me for what you are using this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

IHost.RunAsync() Never Returns
Console output shows that the Worker shuts down and gets disposed, but the application (apparently) terminates before RunAsync() returns.
Read more >
how to run an umbraco cli project on local network
OperationCanceledException: The operation was canceled. at System.Threading. ... RunAsync(IHost host, CancellationToken token) at Microsoft.
Read more >
NET Generic Host in ASP.NET Core
RunAsync. RunAsync runs the app and returns a Task that completes when the cancellation token or shutdown is triggered. RunConsoleAsync.
Read more >
Cannot access a disposed object. Object name: 'System.Net ...
I found a solution. As i understand the listener.GetContextAsync() is a blocking method, and the execution of my code will only continiou when...
Read more >
NET Generic Host
Inject the IHostApplicationLifetime service into any class to handle post-startup and graceful shutdown tasks. Three properties on the interface ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found