{"record":{"id":"ff2fa178bc5f266b","repo":"dotnet/wpf","slug":"sr-mediasystem-outoforderconnectordisconnect","errorCode":null,"errorMessage":"SR.MediaSystem_OutOfOrderConnectOrDisconnect","messagePattern":"SR\\.MediaSystem_OutOfOrderConnectOrDisconnect","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaSystem.cs","lineNumber":192,"sourceCode":"                // Check to see if we need to force software for the Vista Magnifier\n                s_forceSoftareForGraphicsStreamMagnifier =\n                    UnsafeNativeMethods.WgxConnection_ShouldForceSoftwareForGraphicsStreamClient();\n\n                foreach (MediaContext mc in _mediaContexts)\n                {\n                    mc.PostInvalidateRenderMode();\n                }\n            }\n        }\n\n        /// <summary>\n        /// Connect the transport.\n        /// </summary>\n        private static void ConnectTransport()\n        {\n            if (IsTransportConnected)\n            {\n                throw new System.InvalidOperationException(SR.MediaSystem_OutOfOrderConnectOrDisconnect);\n            }\n\n            //\n            // Create a default transport to be used by this media system.\n            // If creation fails, fall back to a local transport.\n            //\n\n            HRESULT.Check(UnsafeNativeMethods.WgxConnection_Create(\n                false, // false means asynchronous transport\n                out s_pConnection));\n\n            // Create service channel used by global glyph cache. This channel is\n            // the first channel created for the app, and by creating it with\n            // a null channel reference it creates a new partition.\n            // All subsequent channel creates will pass in a reference to this\n            // channel thus using its partition.\n            s_serviceChannel = new DUCE.Channel(\n                null,","sourceCodeStart":174,"sourceCodeEnd":210,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaSystem.cs#L174-L210","documentation":"MediaSystem.ConnectTransport throws InvalidOperationException if the media transport is already connected (IsTransportConnected) when a connect is attempted. MediaSystem.Startup is expected to connect the transport exactly once per session; a second connect (without an intervening disconnect) indicates out-of-order media lifecycle calls.","triggerScenarios":"Calling MediaSystem.Startup (directly or via media APIs) when the transport was already connected and not subsequently disconnected - e.g. double startup, or startup after a session that never called Shutdown/DisconnectTransport.","commonSituations":"App-domain reuse or unit tests that call MediaSystem.Startup multiple times without Shutdown; hosting scenarios where WPF media is re-initialized after a partial teardown.","solutions":["Ensure every MediaSystem.Startup has a matching Shutdown before starting again","Check MediaSystem.IsTransportConnected before calling Startup","In tests, call MediaSystem.Shutdown (or recreate the app domain) in teardown"],"exampleFix":"// before\nMediaSystem.Startup();\nMediaSystem.Startup(); // throws if still connected\n// after\nif (!MediaSystem.IsTransportConnected)\n{\n    MediaSystem.Startup();\n}","handlingStrategy":"validation","validationCode":"if (!MediaSystem.IsTransportConnected) MediaSystem.Startup();","typeGuard":null,"tryCatchPattern":"try { MediaSystem.Startup(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"OutOfOrderConnect\")) { /* already started - ignore */ }","preventionTips":["Pair every Startup with a Shutdown","Guard Startup calls with IsTransportConnected","In tests, reset media system state in teardown"],"tags":["wpf","media","lifecycle"],"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"}