{"record":{"id":"47eee2ea72f889af","repo":"cefsharp/CefSharp","slug":"unregisterresourcehandler-can-only-be-used-with-th","errorCode":null,"errorMessage":"UnRegisterResourceHandler can only be used with the default IResourceRequestHandlerFactory(DefaultResourceRequestHandlerFactory) implementation","messagePattern":"UnRegisterResourceHandler can only be used with the default IResourceRequestHandlerFactory\\(DefaultResourceRequestHandlerFactory\\) implementation","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"CefSharp/WebBrowserExtensions.cs","lineNumber":1031,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Unregister a ResourceHandler. Can only be used when browser.ResourceHandlerFactory is an instance of\n        /// DefaultResourceHandlerFactory.\n        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        /// <param name=\"browser\">The ChromiumWebBrowser instance this method extends.</param>\n        /// <param name=\"url\">the url of the resource to unregister.</param>\n        public static void UnRegisterResourceHandler(this IWebBrowser browser, string url)\n        {\n            ThrowExceptionIfChromiumWebBrowserDisposed(browser);\n\n            var handler = browser.ResourceRequestHandlerFactory as ResourceRequestHandlerFactory;\n\n            if (handler == null)\n            {\n                throw new Exception(\"UnRegisterResourceHandler can only be used with the default IResourceRequestHandlerFactory(DefaultResourceRequestHandlerFactory) implementation\");\n            }\n\n            handler.UnregisterHandler(url);\n        }\n\n        /// <summary>\n        /// Stops loading the current page.\n        /// </summary>\n        /// <param name=\"browser\">The ChromiumWebBrowser instance this method extends.</param>\n        public static void Stop(this IChromiumWebBrowserBase browser)\n        {\n            ThrowExceptionIfChromiumWebBrowserDisposed(browser);\n\n            browser.BrowserCore.Stop();\n        }\n\n        /// <summary>\n        /// Stops loading the current page.","sourceCodeStart":1013,"sourceCodeEnd":1049,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp/WebBrowserExtensions.cs#L1013-L1049","documentation":"UnRegisterResourceHandler is a convenience extension that only works against the built-in ResourceRequestHandlerFactory, because it simply removes a URL entry from that factory's internal handler map. The code casts browser.ResourceRequestHandlerFactory to ResourceRequestHandlerFactory; if you supplied your own IResourceRequestHandlerFactory implementation, the cast returns null and the method refuses to operate on an unknown store.","triggerScenarios":"Calling browser.UnRegisterResourceHandler(url) after you previously assigned a custom class implementing IResourceRequestHandlerFactory to browser.ResourceRequestHandlerFactory (instead of the default ResourceRequestHandlerFactory). The 'as' cast at WebBrowserExtensions.cs:1027 yields null, tripping the guard.","commonSituations":"Apps that set a custom IResourceRequestHandlerFactory for fine-grained request interception/control, then try to use the high-level Register/UnRegister/LoadHtml helpers that assume the default factory. Copying sample code that mixes a custom factory with the convenience registration API.","solutions":["If you only need default behavior, do not set a custom factory; let CefSharp assign the default ResourceRequestHandlerFactory (or assign new ResourceRequestHandlerFactory() yourself).","If you must keep a custom IResourceRequestHandlerFactory, manage your own handler registry and unregister there instead of calling UnRegisterResourceHandler.","Check the type before calling: only invoke UnRegisterResourceHandler when browser.ResourceRequestHandlerFactory is ResourceRequestHandlerFactory.","Use RegisterResourceHandler/LoadHtml counterparts consistently; they share the same default-factory requirement."],"exampleFix":"// before\nbrowser.ResourceRequestHandlerFactory = new MyCustomFactory();\n...\nbrowser.UnRegisterResourceHandler(url); // throws\n\n// after (option A): keep default factory\n// do not assign a custom factory; UnRegisterResourceHandler works\nbrowser.UnRegisterResourceHandler(url);\n\n// after (option B): custom factory - unregister in your own implementation\n((MyCustomFactory)browser.ResourceRequestHandlerFactory).Unregister(url);","handlingStrategy":"type-guard","validationCode":"var factory = browser?.ResourceRequestHandlerFactory;\nif (factory is ResourceRequestHandlerFactory)\n{\n    browser.UnRegisterResourceHandler(url);\n}","typeGuard":"static bool UsesDefaultFactory(IWebBrowser browser) =>\n    browser?.ResourceRequestHandlerFactory is ResourceRequestHandlerFactory;","tryCatchPattern":null,"preventionTips":["Decide upfront: either use the default ResourceRequestHandlerFactory for the convenience APIs or implement a custom factory and manage handlers yourself.","Avoid mixing custom factory assignment with Register/UnRegister/LoadHtml helpers.","Centralize handler registration in one place that knows which factory type is in use."],"tags":["cefsharp","resource-handler","configuration","extension-method"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}