dotnet/wpf · error · ArgumentException

SR.CantShowMBServiceWithOwner

Error message

SR.CantShowMBServiceWithOwner

What it means

MessageBox.ShowCore throws ArgumentException when options includes ServiceNotification or DefaultDesktopOnly while an owner window handle (ownerWindow != IntPtr.Zero) is supplied. Service-notification dialogs go to the desktop/service context and cannot have an owner window.

Solutions

  1. Remove the ServiceNotification/DefaultDesktopOnly flags when an owner window is present, or drop the owner parameter.
  2. Use two code paths: owner-based normal dialogs, and ownerless service-notification calls (MessageBox.Show with owner IntPtr.Zero).
  3. If you need both, branch on the flags before calling Show and clear the conflicting owner.

Example fix

// before
MessageBox.Show(ownerWindow, msg, title, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
// after
// owner-based: no service flags
MessageBox.Show(ownerWindow, msg, title, MessageBoxButton.OK, MessageBoxImage.Warning);
// or service notification: no owner
MessageBox.Show(msg, title, MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK, MessageBoxOptions.ServiceNotification);
Defensive patterns

Strategy: validation

Validate before calling

bool isService = (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0;
if (isService && ownerWindow != IntPtr.Zero) options &= ~(MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly);

Type guard

bool CanUseOwner(MessageBoxOptions o) => (o & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0;

Try / catch

try { MessageBox.Show(ownerWindow, msg, title, button, icon, result, options); }
catch (ArgumentException ex) when (ex.Message.Contains("ServiceNotification") || ex.Message.Contains("owner")) { MessageBox.Show(msg, title, button, icon, result, options & ~(MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)); }

Prevention

When it happens

Trigger: Calling MessageBox.Show(Window owner, ...) or an overload passing ownerWindow with options set to MessageBoxOptions.ServiceNotification or MessageBoxOptions.DefaultDesktopOnly.

Common situations: Adding service-notification flags to an existing Show call that already passes a WPF window owner; copy-pasted options from a service codebase into UI code.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/MessageBox.cs:397

            {
                throw new InvalidEnumArgumentException ("defaultResult", (int)defaultResult, typeof(MessageBoxResult));
            }
            if (!IsValidMessageBoxOptions(options))
            {
                throw new InvalidEnumArgumentException("options", (int)options, typeof(MessageBoxOptions));
            }

            // UserInteractive??
            //
            /*if (!SystemInformation.UserInteractive && (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) == 0) {
                throw new InvalidOperationException("UNDONE: SR.GetString(SR.CantShowModalOnNonInteractive)");
            }*/

            if ( (options & (MessageBoxOptions.ServiceNotification | MessageBoxOptions.DefaultDesktopOnly)) != 0)
            {
                if (owner != IntPtr.Zero)
                {
                    throw new ArgumentException(SR.CantShowMBServiceWithOwner);
                }
            }
            else
            {
                if (owner == IntPtr.Zero)
                {
                    owner = UnsafeNativeMethods.GetActiveWindow();
                }
            }

            int style = (int) button | (int) icon | (int) DefaultResultToButtonNumber(defaultResult, button) | (int) options;

            // modal dialog notification?
            //
            //Application.BeginModalMessageLoop();
            //MessageBoxResult result = Win32ToMessageBoxResult(SafeNativeMethods.MessageBox(new HandleRef(owner, handle), messageBoxText, caption, style));
            MessageBoxResult result = Win32ToMessageBoxResult (UnsafeNativeMethods.MessageBox (new HandleRef (null, owner), messageBoxText, caption, style));
            // modal dialog notification?

View on GitHub (pinned to 81131a70a4)