我已经将我的项目复制到一台干净的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引用的版本构建项目。

其他回答

更改我的网页中的绑定信息。config(或app.config) -虽然在我看来是一个“黑客”,允许你在NuGet包更新后继续你的项目,并给你System.Net.Http错误。

设置oldVersion="0.0.0.0-4.1.1.0"和newVersion="4.0.0.0"如下所示

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

如果您的解决方案中有多个项目,那么右键单击Visual Studio中的解决方案图标并选择“管理解决方案的NuGet包”,然后单击第四个选项卡“合并”,将所有项目合并到相同版本的dll中。这将为您提供一个要合并的引用程序集列表。单击列表中的每个项目,然后单击右侧选项卡中的install。

这将在。net 4.7.2和Visual Studio 2017(15.9.4)中工作:

删除web /应用程序。配置绑定重定向 删除System.Net.Http的NuGet包 打开“添加新引用”,直接链接到。net 4.7.2附带的4.2.0.0版本

我有同样的问题,唯一的方法,我如何能够修复它是添加bindingRedirect到app.confing如何写@tripletdad99。

但如果你有更多项目的解决方案,手动更新每个项目(有时更新了一些nuget包后,你需要再做一遍)。这就是为什么我写了简单的powershell脚本,如果所有的app.configs。

 param(
    [string]$SourceDirectory,
    [string]$Package,
    [string]$OldVersion,
    [string]$NewVersion
)

Write-Host "Start fixing app.config in $sourceDirectory"
Write-Host "$Package set oldVersion to $OldVersion and newVersion $NewVersion"
Write-Host "Search app.config files.."
[array]$files = get-childitem $sourceDirectory -Include app.config App.config -Recurse | select -expand FullName
foreach ($file in $files)
{
    Write-Host $file
    $xml = [xml](Get-Content $file)
    $daNodes = $xml.configuration.runtime.assemblyBinding.dependentAssembly
    foreach($node in $daNodes)
    {
        if($node.assemblyIdentity.name -eq $package)
        {
            $updateNode = $node.bindingRedirect
            $updateNode.oldVersion = $OldVersion
            $updateNode.newVersion =$NewVersion
            Write-Host "Fix"
        }
    }
    $xml.Save($file)
}

Write-Host "Done"

使用示例:

./scripts/FixAppConfig.ps1 -SourceDirectory "C:\project-folder" -Package "System.Net.Http" -OldVersion "0.0.0.0-4.3.2.0" -NewVersion "4.0.0.0"

可能它不是完美的,也会更好,如果有人把它链接到预构建任务。

正在使用nuget更新旧网站(包括. net更新和MVC更新)。

我在VS2017中删除了System.Net.HTTP引用(它是版本2.0.0.0),并重新添加了引用,然后显示为4.2.0.0。

然后我用nuget更新了大量的“包”,并得到了错误消息,然后注意到一些东西将引用重置为2.0.0.0,所以我删除并重新添加,它工作正常…怪异。