1

I receive an Exception (Method not found) when attempting to call the following method.

    public static async Task<HttpResponseMessage> Send(HttpRequestMessage request) {

        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

        HttpResponseMessage response = await HttpClient.SendAsync(request);

        if (response.IsSuccessStatusCode)
            return response;

        throw new HttpResponseException(response);
    }

The failure occurs because of the explicit exception throw on the last line.

If I change

throw new HttpResponseException(response);

to

return null;

the method is found.

I'd like to understand the specific reason the method cannot be found when attempting to throw the exception (The generic / async / TaskAwaiter is not able to match all code paths?).

Can you recommend how I can code the method in a way that I can properly throw the exception?

Thanks for your help!

1 Answer 1

4

This turned out to be a red herring. The problem is not related to the method signature, generics, or asynchronous operations. It is instead a .Net Standard Versioning issue the is outlined here:

Why "Method not found: 'Void System.Web.Http.HttpResponseException..ctor" after package update?

Adding the following binding redirect solved the issue:

  <dependentAssembly>
    <assemblyIdentity name="System.Net.Http" culture="neutral" publicKeyToken="b03f5f7f11d50a3a" />
    <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.2.0.0" />
  </dependentAssembly>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.