{"record":{"id":"0322b51653ebec05","repo":"dotnet/wpf","slug":"requesturi-tostring-webexceptionstatus-requestcanceled","errorCode":null,"errorMessage":"requestUri.ToString() (WebExceptionStatus.RequestCanceled)","messagePattern":"requestUri\\.ToString\\(\\) \\(WebExceptionStatus\\.RequestCanceled\\)","errorType":"http","errorClass":"WebException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/WpfWebRequestHelper.cs","lineNumber":87,"sourceCode":"            if (uri.IsFile)\n        {\n            uri = new Uri(uri.GetLeftPart(UriPartial.Path));\n        }\n\n        #pragma warning disable SYSLIB0014 \n        WebRequest request = WebRequest.Create(uri); // CodeQL [SM03781] No practical exfiltration vector, response is unlikely to be relayed back\n        #pragma warning restore SYSLIB0014 \n\n        // It is not clear whether WebRequest.Create() can ever return null, but v1 code make this check in\n        // a couple of places, so it is still done here, just in case.\n        if(request == null)\n        {\n            // Unfortunately, there is no appropriate exception string in PresentationCore, and for v3.5 \n            // we have a total resource freeze. So just report WebExceptionStatus.RequestCanceled:\n            // \"The request was canceled, the WebRequest.Abort method was called, or an unclassifiable error \n            // occurred. This is the default value for Status.\"\n            Uri requestUri = BaseUriHelper.PackAppBaseUri.MakeRelativeUri(uri);\n            throw new WebException(requestUri.ToString(), WebExceptionStatus.RequestCanceled);\n            //throw new IOException(SR.Format(SR.GetResponseFailed, requestUri.ToString()));\n        }\n        \n        HttpWebRequest httpRequest = request as HttpWebRequest;\n        if (httpRequest != null)\n        {\n            if (string.IsNullOrEmpty(httpRequest.UserAgent))\n            {\n                httpRequest.UserAgent = DefaultUserAgent;\n            }\n\n            CookieHandler.HandleWebRequest(httpRequest);\n\n            // Enable NTLM/Kerberos/Negotiate authentication only when the target URI\n            // is in a trusted security zone (Local Machine / Intranet / Trusted).\n            //\n            // On .NET Framework, this gate was provided by registering an\n            // ICredentialPolicy with AuthenticationManager.CredentialPolicy, which","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/WpfWebRequestHelper.cs#L69-L105","documentation":"WpfWebRequestHelper.CreateRequest throws a WebException with status WebExceptionStatus.RequestCanceled when the requested URI cannot be resolved against the application's pack:// base URI (the MakeRelativeUri result is used only as the exception message). The comment notes no suitable exception string existed in PresentationCore, so RequestCanceled is reported as a catch-all for unsupported/invalid request URIs.","triggerScenarios":"Calling WpfWebRequestHelper.CreateRequest (or APIs like Application.GetResourceStream/GetContentStream/GetRemoteStream that use it) with a URI that is not a valid relative or pack-absolute resource URI, e.g. a malformed relative path or a scheme the helper cannot build a WebRequest for.","commonSituations":"Loading resource/content/siteoforigin files with wrong relative paths, uppercase/space escaping problems in pack URIs, or requesting URIs after moving resources so they no longer resolve under PackAppBaseUri.","solutions":["Validate the URI resolves correctly against the pack application base (use Uri.TryCreate with UriKind.RelativeOrAbsolute and check the file exists in the build output / is packed as Resource).","Use BaseUriHelper.PackAppBaseUri.MakeRelativeUri yourself in a try/catch to diagnose which URI fails.","Fix pack URI syntax (pack://application:,,,/Assembly;component/Path) and ensure the resource Build Action is Resource/Content as appropriate.","Catch WebException and inspect Status == WebExceptionStatus.RequestCanceled to give the user a clearer message."],"exampleFix":"// before\nvar resp = WpfWebRequestHelper.CreateRequest(new Uri(userInput));\n// after\nif (!Uri.TryCreate(userInput, UriKind.RelativeOrAbsolute, out var uri))\n    throw new InvalidOperationException(\"Invalid resource URI: \" + userInput);\nvar resp = WpfWebRequestHelper.CreateRequest(new Uri(BaseUriHelper.PackAppBaseUri, uri));","handlingStrategy":"validation","validationCode":"bool IsValidResourceUri(Uri uri)\n{\n    if (uri == null || !uri.IsWellFormedOriginalString()) return false;\n    if (uri.IsAbsoluteUri && uri.Scheme != \"pack\") return false;\n    try { BaseUriHelper.PackAppBaseUri.MakeRelativeUri(uri); }\n    catch { return false; }\n    return true;\n}","typeGuard":"bool IsPackUri(Uri u) => u != null && (!u.IsAbsoluteUri || u.Scheme == \"pack\");","tryCatchPattern":"try { return WpfWebRequestHelper.CreateRequest(uri); }\ncatch (WebException ex) when (ex.Status == WebExceptionStatus.RequestCanceled)\n{ throw new InvalidOperationException(\"Cannot resolve resource URI: \" + uri, ex); }","preventionTips":["Use pack://application:,,,/... syntax and verify Build Action (Resource/Content).","Test resource loading in CI so moved/renamed resources fail early.","Always create relative URIs against BaseUriHelper.PackAppBaseUri."],"tags":["web-exception","pack-uri","wpf","resource-loading","network"],"backgroundTag":"invalid-url","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"}