ObjectDisposedException when cancelling IHost.RunAsync()
See original GitHub issueStack 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:
- Created 2 years ago
- Reactions:1
- Comments:5 (5 by maintainers)
Top GitHub Comments
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.
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?