dotnet/wpf · error · InvalidOperationException

SR.MultiSingleton

Error message

SR.MultiSingleton

What it means

The Application constructor enforces the single-instance-per-AppDomain rule. If an Application instance already exists in the current AppDomain (checked under a lock), the second construction throws InvalidOperationException with SR.MultiSingleton. WPF requires exactly one Application object per AppDomain.

Solutions

  1. Ensure Application.Current is used instead of constructing a new Application
  2. Allow the existing Application to shut down (Application.Current.Shutdown) and its AppDomain to be recycled before creating a new one
  3. In tests, create Application once in a fixture and share it, or dispose via Dispatcher shutdown between tests
  4. Check Application.Current != null before calling new Application()

Example fix

// before
var app = new Application(); // throws if one exists

// after
var app = Application.Current ?? new Application();
Defensive patterns

Strategy: type-guard

Validate before calling

if (Application.Current == null) { app = new Application(); }

Type guard

Application GetOrCreateApp() => Application.Current ?? new Application();

Try / catch

Application app;
try { app = new Application(); }
catch (InvalidOperationException) { app = Application.Current; }

Prevention

When it happens

Trigger: Calling new Application() a second time in the same AppDomain, whether in startup code, a unit test that does not tear down the first instance, or a plugin loaded into an already-running WPF AppDomain.

Common situations: Unit tests constructing Application multiple times without shutdown/cleanup; hosting WPF in a plug-in system where the host already created Application; accidentally re-running Main-like bootstrap code.

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/c3e03fe2f8daba8e. Report an issue: GitHub.

Appendix: source

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

#endif // DEBUG_CLR_MEM

            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordGeneral | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientAppCtor);

            lock(_globalLock)
            {
                // set the default statics
                // DO NOT move this from the begining of this constructor
                if (!_appCreatedInThisAppDomain)
                {
                    Debug.Assert(_appInstance == null, "_appInstance must be null here.");
                    _appInstance = this;
                    IsShuttingDown    = false;
                    _appCreatedInThisAppDomain = true;
                }
                else
                {
                    //lock will be released, so no worries about throwing an exception inside the lock
                    throw new InvalidOperationException(SR.MultiSingleton);
                }
            }


            //
            // (Application not shutting down when calling
            // Application.Current.Shutdown())
            //
            // post item to do startup work
            // posting it here so that this is the first item in the queue. Devs
            // could post items before calling run and then those will be serviced
            // before if we don't post this one here.
            //
            // Also, doing startup (firing OnStartup etc.) once our dispatcher
            // is run ensures that we run before any external code is run in the
            // application's Dispatcher.
            Dispatcher.BeginInvoke(
                DispatcherPriority.Send,

View on GitHub (pinned to 81131a70a4)