dotnet/wpf · error · InvalidOperationException

SR.ApplicationShuttingDown

Error message

SR.ApplicationShuttingDown

What it means

GetResourcePackage reached a state where the requested package was not found while the application is shutting down; an Invariant.Assert confirmed IsApplicationObjectShuttingDown, and the method then throws InvalidOperationException(SR.ApplicationShuttingDown). This is effectively an internal invariant path: during shutdown, resource packages are being torn down, so the package is no longer available.

Solutions

  1. Check Application.Current == null or Dispatcher.HasShutdownStarted before resolving resources on background code
  2. Cancel outstanding async resource loads in OnExit / ShutdownStarted
  3. Load needed resources before initiating Shutdown, and cache streams
  4. Marshal shutdown-sensitive work onto the dispatcher before shutdown starts; guard with try-catch on InvalidOperationException in teardown paths

Example fix

// before
var s = Application.GetContentStream(uri); // may hit shutdown teardown
// after
if (Application.Current != null && !Application.Current.Dispatcher.HasShutdownStarted)
    var s = Application.GetContentStream(uri);
Defensive patterns

Strategy: try-catch

Validate before calling

bool canLoadResources() => Application.Current != null && !Application.Current.Dispatcher.HasShutdownStarted;

Type guard

bool AppAlive(Application app) => app is not null && !app.Dispatcher.HasShutdownStarted;

Try / catch

try { var s = Application.GetContentStream(uri); } catch (InvalidOperationException) { /* app is shutting down; abort resource load */ }

Prevention

When it happens

Trigger: Requesting a resource/package (via pack URI resolution, GetContentStream/GetRemoteStream internals) after Shutdown began; background threads still resolving pack URIs while the Exit event fires; image bindings resolving sources during window close.

Common situations: Async image loads racing application shutdown; timer/dispatcher callbacks firing after Shutdown; pack URI access inside Exit handlers; leaks where a background worker outlives the app lifetime.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9758da994145f00e. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs:2043

                part = resContainer.GetPart(partUri);
            }

            return part;
        }

        /// <summary> Helper for getting the pack://application or pack://siteoforigin resource package. </summary>
        /// <param name="packageUri"> "application://" or "siteoforigin://" </param>
        private static Package GetResourcePackage(Uri packageUri)
        {
            Package package = PreloadedPackages.GetPackage(packageUri);
            if (package == null)
            {
                Uri packUri = PackUriHelper.Create(packageUri);
                Invariant.Assert(packUri == BaseUriHelper.PackAppBaseUri || packUri == BaseUriHelper.SiteOfOriginBaseUri,
                    $"Unknown packageUri passed: {packageUri}");

                Invariant.Assert(IsApplicationObjectShuttingDown);
                throw new InvalidOperationException(SR.ApplicationShuttingDown);
            }
            return package;
        }

        /// <summary>
        ///     Creates hwndsource so that we can listen to some window msgs.
        /// </summary>
        private void EnsureHwndSource()
        {
            if (_parkingHwnd == null)
            {
                // _appFilterHook needs to be member variable otherwise
                // it is GC'ed and we don't get messages from HwndWrapper
                // (HwndWrapper keeps a WeakReference to the hook)

                _appFilterHook = new HwndWrapperHook(AppFilterMessage);
                HwndWrapperHook[] wrapperHooks = {_appFilterHook};

View on GitHub (pinned to 81131a70a4)