locked
Custom httpClient filter through exception of type system.AccessViolationException when server is down

    Question

  • I am trying to add custom filter to HttpClient it works fine but when the server is down the catch block return null which causes the system to through System.AccessViolationException. how should i handle that case.

    public IAsyncOperationWithProgress<HttpResponseMessage, HttpProgress> SendRequestAsync(HttpRequestMessage request) { HttpResponseMessage response = new HttpResponseMessage(); return AsyncInfo.Run<HttpResponseMessage, HttpProgress>(async (cancellationToken, progress) => { if ((App.userData != null && !string.IsNullOrWhiteSpace(App.userData.Token)) || App.Get_user()) { request.Headers.Add("AuthorizationHeader", App.userData.Token); } try {

    // her the exception fires if the server is offline.

    response = await innerFilter.SendRequestAsync(request); if (response.StatusCode == HttpStatusCode.Forbidden) { ((Frame)Window.Current.Content).Navigate(typeof(LoginPage)); return response; } return response; } catch { return null; } }); }

    and her were i call var response = await _client.GetAsync(value); which in turn will fire SendRequestAsync.


    Wednesday, February 25, 2015 3:42 PM

Answers

  • You are getting an AccessViolationException because you returned null.  So response will be null, and if you try and access a property off of it you will get this exception.  Check for response == null instead of just using it:

    var response = await _client.GetAsync(value); 
    if(response != null)
    {
        // valid response do something
    }


    Bret Bentzinger (MSFT) @awehellyeah

    Thursday, February 26, 2015 4:14 AM
    Moderator