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

3
我已验证了以HttpResponseMessage作为返回类型的文件被设置为“编译”,如他们在这里所说:为什么我不能使用HttpResponseMessage类? 我已从packages.config迁移到packagereference(nuget),并拥有一个包含webapi的.net 4.7.2 mvc项目。虽然它之前运行正常,但在几次Nuget更新后就不再工作了。我曾遇到过system.web.http和框架中4.0.0版本及更新的4.x nuget的问题。
我的问题似乎与https://github.com/Microsoft/dotnet/issues/737有关,但我不确定。
我还尝试过https://dev59.com/t2Ij5IYBdhLWcg3wfVB8#50095678
因此,以下是我的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后,Intellisense会建议添加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包,但即使已安装了4.3.0 NuGet,我仍然卡在System.Runtime,Version=4.2.1.0上。通常,VS会在调试信息中给我一个双击并在web.config中重定向程序集绑定的选项。但它不在那里,所以可能需要手动添加?
我还尝试添加

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

添加到所有其他NuGet的包引用中。但这也没有帮助。我还尝试添加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, Version=4.2.1.0

2个回答

6

David,问题解决了:https://github.com/dotnet/corefx/issues/29622 即使在我升级到15.8之前就已经解决了。 - JP Hellemons

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

正如所说,重定向不起作用,我已经安装了4.3版本,因此应该使用4.3版本。 - JP Hellemons
哦,而且NuGet只列出了4.3和4.1版本,所以在NuGet源中没有4.2.x的包。 - JP Hellemons
如何解决这个问题? - Derek R

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接