我已经将我的项目复制到一台干净的Windows 10机器上,只安装了Visual Studio 2015 Community和SQL Server 2016 Express。除了Windows 10和VS2015或SQL Server安装的框架版本外,没有安装其他框架版本。

当我尝试启动WebApi项目时,我得到了这样的消息:

无法加载文件或程序集"System.Net。Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。系统无法找到指定的文件。

项目包包括:

<package id="Microsoft.AspNet.WebApi" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.Tracing" version="5.2.3" targetFramework="net45" />
<package id="Microsoft.AspNet.WebApi.WebHost" version="5.2.3" targetFramework="net45" />

在使用。net Framework 4.6.1构建项目后,在bin文件夹中找不到System.Net.Http文件。

文件的路径指向:

C:\Program Files (x86)\Reference程序集\Microsoft\ framework.net框架\v4.6.1\System.Net.Http.dll

System.Net.Http.Formatting的文件路径指向:

C: \开发\ MyApp \ \ Microsoft.AspNet.WebApi.Client.5.2.3 \ lib \ net45 \ System.Net.Http.Formatting.dll包

整个项目应该以4.5.1为目标,还是有其他方法来引用正确的程序集?


当前回答

4.6.1-2在VS2017中,用户可能会遇到他们的System.Net.Http版本被VS2017或Msbuild 15想要使用的版本替换。

我们在这里删除了这个版本:

C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

在这里:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

然后用我们通过NuGet引用的版本构建项目。

其他回答

我有这个,但是,这是因为我添加了一个NuGet包,更新了绑定重定向。一旦我删除了包,重定向仍然在那里。我删除了所有这些,然后运行update-package -重装。这增加了正确的重定向。

对我来说,唯一干净利落地解决这个问题的方法。NET 4.6.1)不仅为实际使用System.Net的项目添加了对System.Net.Http V4.3.4的Nuget引用。Http,但也启动项目(在我的情况下是一个测试项目)。

(这很奇怪,因为正确的System.Net.Http.dll存在于测试项目的bin目录中,.config assemblyBingings看起来也不错。)

删除name="System.Net. "的dependentAssembly。Http”来自web。Config对我也有用。我从网上评论了这部分。配置,它为我工作。 如果以上所有解决方案都不适用,请尝试如下所示的注释或删除。

<!--<dependentAssembly>
        <assemblyIdentity name="System.Net.Http" publicKeyToken="b03f5f7f11d50a3a" culture="neutral"/>
        <bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.0.0.0"/>
      </dependentAssembly>-->

在做一些技巧和复杂的配置之前,尝试删除bin和obj文件夹,然后进行编译。解决了同样的问题

变更后:

<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.1.1.2" />

与以下:

<bindingRedirect oldVersion="0.0.0.0-4.1.1.2" newVersion="4.0.0.0" />

在web . config