【问题标题】:Extension method not found when publishing ASP.NET MVC app发布 ASP.NET MVC 应用程序时找不到扩展方法
【发布时间】:2019-04-23 16:57:41
【问题描述】:

在 ASP.NET MVC 应用程序 (.NET 4.7) 中,我在 HttpResponseMessage 上使用扩展方法。此方法是在 .NET Standard 2.0 项目中创建的。在调试和使用 IIS Express 时,它工作得很好。

但是当发布到服务器时,它返回“找不到方法”错误。在服务器 (Windows Server 2008) 上安装了所有需要的框架。

当我在调用扩展方法的方法中使用 try/catch 时,错误变得清晰。

在开发中,我希望它可以在服务器上运行。我是否缺少任何其他 .NET 框架?或者还有其他人作为可能的解决方案吗?

HttpResponseMessage response = client.GetAsync(UserManAPI).Result;
if (response.IsSuccessStatusCode)
{
    apiResponse = response.Content.ReadAsStringAsync().Result;
}
else
    response.ThrowReynaersException();


public static void ThrowReynaersException(this HttpResponseMessage response)
{
        if (response.StatusCode == HttpStatusCode.OK)
        {
            using (var dataStream = 
                    response.Content.ReadAsStreamAsync().Result)
            {
                 if (dataStream != null)
                 {
                      var jsonResponse = JsonValue.Load(dataStream);
                      if (jsonResponse != null)
                      {
                           var rex = 
               JsonConvert.DeserializeObject<ReynaersException(jsonResponse,
                            new ReynaersExceptionConverter());
                           if (rex != null) throw rex;
                           var ex = JsonConvert.DeserializeObject<Exception>(jsonResponse, new ExceptionConverter());
                           if (ex != null) throw ex.ToReynaersException(); 
                           throw new 
                        ReynaersException(ErrorResourcesKeys.DefaultMessage);
                    }
                }
            }
        }
    }

提前致谢!

【问题讨论】:

  • 我建议将您的代码添加到您的问题中。这将增加获得答案的机会,并减少您的问题被标记和删除的变化。

标签: c# asp.net-mvc extension-methods .net-standard-2.0 httpresponsemessage


【解决方案1】:

在 web.config 中尝试此操作或使用 Nuget 包重新安装“System.Net.Http”

<configuration>
    <system.web>
         <compilation>
             <assemblies>
                <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
             </assemblies>
          </compilation>
    </system.web>
</configuration>

【讨论】:

  • 将 System.Net.Http 安装为 Nuget 包,在 web.config 中添加了一个dependentAssembly 解决了这个问题。谢谢@Marcelo Vismari
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-02-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多