dotnet/wpf · error · WebException

requestUri.ToString() (WebExceptionStatus.RequestCanceled)

Error message

requestUri.ToString() (WebExceptionStatus.RequestCanceled)

What it means

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.

Solutions

  1. 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).
  2. Use BaseUriHelper.PackAppBaseUri.MakeRelativeUri yourself in a try/catch to diagnose which URI fails.
  3. Fix pack URI syntax (pack://application:,,,/Assembly;component/Path) and ensure the resource Build Action is Resource/Content as appropriate.
  4. Catch WebException and inspect Status == WebExceptionStatus.RequestCanceled to give the user a clearer message.

Example fix

// before
var resp = WpfWebRequestHelper.CreateRequest(new Uri(userInput));
// after
if (!Uri.TryCreate(userInput, UriKind.RelativeOrAbsolute, out var uri))
    throw new InvalidOperationException("Invalid resource URI: " + userInput);
var resp = WpfWebRequestHelper.CreateRequest(new Uri(BaseUriHelper.PackAppBaseUri, uri));
Defensive patterns

Strategy: validation

Validate before calling

bool IsValidResourceUri(Uri uri)
{
    if (uri == null || !uri.IsWellFormedOriginalString()) return false;
    if (uri.IsAbsoluteUri && uri.Scheme != "pack") return false;
    try { BaseUriHelper.PackAppBaseUri.MakeRelativeUri(uri); }
    catch { return false; }
    return true;
}

Type guard

bool IsPackUri(Uri u) => u != null && (!u.IsAbsoluteUri || u.Scheme == "pack");

Try / catch

try { return WpfWebRequestHelper.CreateRequest(uri); }
catch (WebException ex) when (ex.Status == WebExceptionStatus.RequestCanceled)
{ throw new InvalidOperationException("Cannot resolve resource URI: " + uri, ex); }

Prevention

When it happens

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

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

Understand the failure class

Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/WpfWebRequestHelper.cs:87

            if (uri.IsFile)
        {
            uri = new Uri(uri.GetLeftPart(UriPartial.Path));
        }

        #pragma warning disable SYSLIB0014 
        WebRequest request = WebRequest.Create(uri); // CodeQL [SM03781] No practical exfiltration vector, response is unlikely to be relayed back
        #pragma warning restore SYSLIB0014 

        // It is not clear whether WebRequest.Create() can ever return null, but v1 code make this check in
        // a couple of places, so it is still done here, just in case.
        if(request == null)
        {
            // Unfortunately, there is no appropriate exception string in PresentationCore, and for v3.5 
            // we have a total resource freeze. So just report WebExceptionStatus.RequestCanceled:
            // "The request was canceled, the WebRequest.Abort method was called, or an unclassifiable error 
            // occurred. This is the default value for Status."
            Uri requestUri = BaseUriHelper.PackAppBaseUri.MakeRelativeUri(uri);
            throw new WebException(requestUri.ToString(), WebExceptionStatus.RequestCanceled);
            //throw new IOException(SR.Format(SR.GetResponseFailed, requestUri.ToString()));
        }
        
        HttpWebRequest httpRequest = request as HttpWebRequest;
        if (httpRequest != null)
        {
            if (string.IsNullOrEmpty(httpRequest.UserAgent))
            {
                httpRequest.UserAgent = DefaultUserAgent;
            }

            CookieHandler.HandleWebRequest(httpRequest);

            // Enable NTLM/Kerberos/Negotiate authentication only when the target URI
            // is in a trusted security zone (Local Machine / Intranet / Trusted).
            //
            // On .NET Framework, this gate was provided by registering an
            // ICredentialPolicy with AuthenticationManager.CredentialPolicy, which

View on GitHub (pinned to 81131a70a4)