【问题标题】:C# Method Not Found when async returning Exception异步返回异常时未找到 C# 方法
【发布时间】:2018-04-20 14:16:27
【问题描述】:

我在尝试调用以下方法时收到异常(找不到方法)。

    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);
    }

由于最后一行显式抛出异常而发生故障。

如果我改变了

throw new HttpResponseException(response);

return null;

找到方法。

我想了解尝试抛出异常时找不到方法的具体原因(通用/异步/TaskAwaiter无法匹配所有代码路径?)。

您能否推荐我如何以可以正确抛出异常的方式对方法进行编码?

感谢您的帮助!

【问题讨论】:

    标签: generics asynchronous exception missingmethodexception


    【解决方案1】:

    结果证明这是一条红鲱鱼。该问题与方法签名、泛型或异步操作无关。相反,它是一个 .Net 标准版本控制问题,此处概述:

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

    添加以下绑定重定向解决了这个问题:

      <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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-06-29
      • 2018-10-23
      • 1970-01-01
      • 2013-05-03
      • 2012-12-20
      • 2014-10-22
      • 2018-06-10
      相关资源
      最近更新 更多