代码之家  ›  专栏  ›  技术社区  ›  JP Hellemons

找不到类型或命名空间名称“HttpResponseMessage”

  •  2
  • JP Hellemons  · 技术社区  · 7 年前

    我已经确认了 HttpResponseMessage 因为返回类型被设置为“compile”,就像他们在这里说的: Why I cannot use HttpResponseMessage class?

    我已经从packages.config迁移到packagereference(nuget),并且有一个包含webapi的.net 4.7.2mvc项目。它以前工作过,但经过几次Nuget更新之后就不再工作了。我对system.web.http和框架中的内置4.0.0版本以及较新的4.x nuget有问题。

    我的问题似乎与 https://github.com/Microsoft/dotnet/issues/737 但我不确定。

    我也试过这个 https://stackoverflow.com/a/50095678/169714

    这是我web.config的一部分:

    <system.web>
    <globalization fileEncoding="utf-8" culture="auto" uiCulture="auto" enableClientBasedCulture="true"/>
    <compilation debug="true" targetFramework="4.7.2">
      <assemblies>
        <add assembly="System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>
      </assemblies>
    </compilation>
    <httpRuntime targetFramework="4.7.2"/>
    

    切换回4.7.1是明智的,建议添加 using System.Net.Http; 而不是 using System.Web.Http; 似乎确实能解决这个问题。但为什么呢?

    所以我现在可以发布“答案”:回复到4.7.1。所以问题是:为什么会这样?

    编辑

    我已手动从packages.config移到packagereference,发现: Visual Studio 2017 - Could not load file or assembly 'System.Runtime, Version=4.1.0.0' or one of its dependencies 据说应该有迁移的选择。但我没有看到它,并删除了.config文件。我已经重新安装了大部分nuget包,但是我仍然坚持 System.Runtime, Version=4.2.1.0 即使我安装了4.3.0Nuget。通常,VS会在debug info中给我一个选项,让我双击web.config中的程序集绑定并重定向。但它不在那里,所以也许是手动添加?

    我也试着加上

     <ItemGroup>
      <PackageReference Include="Legacy2CPSWorkaround" Version="1.0.0">
        <PrivateAssets>All</PrivateAssets>
      </PackageReference>
    </ItemGroup>
    

    所有其他nuget的packagereference。但这也没有帮助。我还试图添加NetStandard.Library nuget,但这也没有帮助。。。

    添加

    <dependentAssembly>
      <assemblyIdentity name="System.Runtime" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
      <bindingRedirect oldVersion="0.0.0.0-4.1.1.0" newVersion="4.3.0" />
    </dependentAssembly>
    

    这也没有帮助。以前web.config中没有system.runtime。它仍在寻找 System.Runtime,版本=4.2.1.0 .

    2 回复  |  直到 7 年前
        1
  •  4
  •   David - MSFT    7 年前

    在.NET 4.7.2上不需要包或绑定重定向。简单地引用System.Net.Http并使用System.Net.Http应该可以使HttpResponseMessage可用。

    这个问题是相关的: https://github.com/dotnet/sdk/issues/2221

    请安装Visual Studio 2017更新15.8。它将确保项目文件和依赖项适用于您的场景。

    见: https://blogs.msdn.microsoft.com/visualstudio/2018/08/14/visual-studio-2017-version-15-8/

        2
  •  0
  •   JMacSoftware    7 年前

    如果你有能力安装它正在寻找的软件包,我总是这样做。在通过Visual Studio的NuGet命令提示符中使用“Install Package System.Runtime-Version 4.2.1”通常可以做到这一点并消除错误,否则您将不得不像尝试那样重定向到4.3.0。希望这有帮助!