{"record":{"id":"4de54414a887fe06","repo":"dotnet/wpf","slug":"sr-webbrowsernocasttoiwebbrowser2","errorCode":null,"errorMessage":"SR.WebBrowserNoCastToIWebBrowser2","messagePattern":"SR\\.WebBrowserNoCastToIWebBrowser2","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"critical","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs","lineNumber":653,"sourceCode":"\n        [DebuggerBrowsable(DebuggerBrowsableState.Never)]\n        internal UnsafeNativeMethods.IWebBrowser2 AxIWebBrowser2\n        {\n            get\n            {\n                if (_axIWebBrowser2 == null)\n                {\n                    ObjectDisposedException.ThrowIf(IsDisposed, this);\n\n                    //This should call AttachInterfaces which will set this member variable\n                    //We don't want to force the state to InPlaceActive yet since we don't\n                    //have the parent handle yet.\n                    TransitionUpTo(ActiveXHelper.ActiveXState.Running);\n                }\n                // We still don't have _axIWebBrowser2. Throw an exception.\n                if (_axIWebBrowser2 == null)\n                {\n                    throw new InvalidOperationException(SR.WebBrowserNoCastToIWebBrowser2);\n                }\n                return _axIWebBrowser2;\n            }\n        }\n\n        internal WebOCHostingAdaptor HostingAdaptor { get { return _hostingAdaptor; } }\n\n        internal Stream DocumentStream\n        {\n            get\n            {\n                return _documentStream;\n            }\n            set\n            {\n                _documentStream = value;\n            }\n        }","sourceCodeStart":635,"sourceCodeEnd":671,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/WebBrowser.cs#L635-L671","documentation":"The WebBrowser's internal accessor throws InvalidOperationException (SR.WebBrowserNoCastToIWebBrowser2) when the hosted ActiveX/WebOC control exists but could not be cast to IWebBrowser2, so the underlying browser interface is unavailable after transitioning to the Running state.","triggerScenarios":"Accessing WebBrowser members that require _axIWebBrowser2 (navigation, InvokeScript, etc.) when ActiveX hosting failed to initialize — e.g. broken IE/Edge-WebView2 runtime registration, hosting in an unsupported environment, or activation failure.","commonSituations":"IE11/MSHTML not installed or disabled on the machine; hosting WebBrowser in non-WPF-window contexts (offscreen, service); registry Feature Browser Emulation issues; OS updates removing legacy WebBrowser support.","solutions":["Ensure the target machine has Internet Explorer components/MSHTML available (Enable-VBR / verify ieframe.dll registration).","Verify the WebBrowser control is hosted in a real, loaded WPF visual tree (Window shown) before use.","Check registry key HKLM\\...\\Internet Explorer\\Main\\FeatureControl\\FEATURE_BROWSER_EMULATION for conflicts.","Consider migrating to WebView2 (Microsoft.Web.WebView2.Wpf) since the legacy WebBrowser control is deprecated.","Wrap access in try-catch (InvalidOperationException) and surface a clear environment error."],"exampleFix":"// before\nwebBrowser.Navigate(\"https://example.com\"); // may throw if IWebBrowser2 unavailable\n// after\ntry { webBrowser.Navigate(\"https://example.com\"); }\ncatch (InvalidOperationException ex)\n{\n    // log: legacy WebBrowser hosting failed; consider WebView2 fallback\n}","handlingStrategy":"try-catch","validationCode":"if (!Environment.OSVersion.Platform.Equals(PlatformID.Win32NT)) skip legacy WebBrowser; ensure control is in a loaded visual tree;","typeGuard":null,"tryCatchPattern":"try { webBrowser.Navigate(url); } catch (InvalidOperationException ex) { Log(ex); UseWebView2Fallback(url); }","preventionTips":["Verify MSHTML/IE components exist on target machines","Host the control in a shown Window before navigating","Plan WebView2 migration for long-term support"],"tags":["wpf","webbrowser","activex","interop","environment"],"backgroundTag":"internal-invariant-violation","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"}