dotnet/wpf · error · InvalidOperationException

SR.MediaSystem_OutOfOrderConnectOrDisconnect

Error message

SR.MediaSystem_OutOfOrderConnectOrDisconnect

What it means

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.

Solutions

  1. Ensure every MediaSystem.Startup has a matching Shutdown before starting again
  2. Check MediaSystem.IsTransportConnected before calling Startup
  3. In tests, call MediaSystem.Shutdown (or recreate the app domain) in teardown

Example fix

// before
MediaSystem.Startup();
MediaSystem.Startup(); // throws if still connected
// after
if (!MediaSystem.IsTransportConnected)
{
    MediaSystem.Startup();
}
Defensive patterns

Strategy: validation

Validate before calling

if (!MediaSystem.IsTransportConnected) MediaSystem.Startup();

Try / catch

try { MediaSystem.Startup(); } catch (InvalidOperationException ex) when (ex.Message.Contains("OutOfOrderConnect")) { /* already started - ignore */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/MediaSystem.cs:192

                // Check to see if we need to force software for the Vista Magnifier
                s_forceSoftareForGraphicsStreamMagnifier =
                    UnsafeNativeMethods.WgxConnection_ShouldForceSoftwareForGraphicsStreamClient();

                foreach (MediaContext mc in _mediaContexts)
                {
                    mc.PostInvalidateRenderMode();
                }
            }
        }

        /// <summary>
        /// Connect the transport.
        /// </summary>
        private static void ConnectTransport()
        {
            if (IsTransportConnected)
            {
                throw new System.InvalidOperationException(SR.MediaSystem_OutOfOrderConnectOrDisconnect);
            }

            //
            // Create a default transport to be used by this media system.
            // If creation fails, fall back to a local transport.
            //

            HRESULT.Check(UnsafeNativeMethods.WgxConnection_Create(
                false, // false means asynchronous transport
                out s_pConnection));

            // Create service channel used by global glyph cache. This channel is
            // the first channel created for the app, and by creating it with
            // a null channel reference it creates a new partition.
            // All subsequent channel creates will pass in a reference to this
            // channel thus using its partition.
            s_serviceChannel = new DUCE.Channel(
                null,

View on GitHub (pinned to 81131a70a4)