{"record":{"id":"c3e03fe2f8daba8e","repo":"dotnet/wpf","slug":"sr-multisingleton","errorCode":null,"errorMessage":"SR.MultiSingleton","messagePattern":"SR\\.MultiSingleton","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs","lineNumber":115,"sourceCode":"#endif // DEBUG_CLR_MEM\n\n            EventTrace.EasyTraceEvent(EventTrace.Keyword.KeywordGeneral | EventTrace.Keyword.KeywordPerf, EventTrace.Event.WClientAppCtor);\n\n            lock(_globalLock)\n            {\n                // set the default statics\n                // DO NOT move this from the begining of this constructor\n                if (!_appCreatedInThisAppDomain)\n                {\n                    Debug.Assert(_appInstance == null, \"_appInstance must be null here.\");\n                    _appInstance = this;\n                    IsShuttingDown    = false;\n                    _appCreatedInThisAppDomain = true;\n                }\n                else\n                {\n                    //lock will be released, so no worries about throwing an exception inside the lock\n                    throw new InvalidOperationException(SR.MultiSingleton);\n                }\n            }\n\n\n            //\n            // (Application not shutting down when calling\n            // Application.Current.Shutdown())\n            //\n            // post item to do startup work\n            // posting it here so that this is the first item in the queue. Devs\n            // could post items before calling run and then those will be serviced\n            // before if we don't post this one here.\n            //\n            // Also, doing startup (firing OnStartup etc.) once our dispatcher\n            // is run ensures that we run before any external code is run in the\n            // application's Dispatcher.\n            Dispatcher.BeginInvoke(\n                DispatcherPriority.Send,","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Application.cs#L97-L133","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Ensure Application.Current is used instead of constructing a new Application","Allow the existing Application to shut down (Application.Current.Shutdown) and its AppDomain to be recycled before creating a new one","In tests, create Application once in a fixture and share it, or dispose via Dispatcher shutdown between tests","Check Application.Current != null before calling new Application()"],"exampleFix":"// before\nvar app = new Application(); // throws if one exists\n\n// after\nvar app = Application.Current ?? new Application();","handlingStrategy":"type-guard","validationCode":"if (Application.Current == null) { app = new Application(); }","typeGuard":"Application GetOrCreateApp() => Application.Current ?? new Application();","tryCatchPattern":"Application app;\ntry { app = new Application(); }\ncatch (InvalidOperationException) { app = Application.Current; }","preventionTips":["Use Application.Current instead of constructing Application directly","In test suites, share a single Application fixture or shut down the Dispatcher between tests","Do not bootstrap WPF twice in plug-in/host scenarios"],"tags":["wpf","application-lifetime","singleton"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}