我已经将我的项目复制到一台干净的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为目标,还是有其他方法来引用正确的程序集?


当前回答

我有同样的问题,唯一的方法,我如何能够修复它是添加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"

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

其他回答

检查.net框架版本。 我最初的。net框架是旧版本。 当我安装了。net framework 4.6之后,这个问题就自动解决了。

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

对我来说,我又加了一块鸡块,问题就解决了

你可以通过将项目升级到. net Framework 4.7.2来解决这个问题。这个问题由微软的Alex giondea回答。请给他投票,因为他真的值得!

This is documented as a known issue in .NET Framework 4.7.1. As a workaround you can add these targets to your project. They will remove the DesignFacadesToFilter from the list of references passed to SGEN (and add them back once SGEN is done) <Target Name="RemoveDesignTimeFacadesBeforeSGen" BeforeTargets="GenerateSerializationAssemblies"> <ItemGroup> <DesignFacadesToFilter Include="System.IO.Compression.ZipFile" /> <_FilterOutFromReferencePath Include="@(_DesignTimeFacadeAssemblies_Names->'%(OriginalIdentity)')" Condition="'@(DesignFacadesToFilter)' == '@(_DesignTimeFacadeAssemblies_Names)' and '%(Identity)' != ''" /> <ReferencePath Remove="@(_FilterOutFromReferencePath)" /> </ItemGroup> <Message Importance="normal" Text="Removing DesignTimeFacades from ReferencePath before running SGen." /> </Target> <Target Name="ReAddDesignTimeFacadesBeforeSGen" AfterTargets="GenerateSerializationAssemblies"> <ItemGroup> <ReferencePath Include="@(_FilterOutFromReferencePath)" /> </ItemGroup> <Message Importance="normal" Text="Adding back DesignTimeFacades from ReferencePath now that SGen has ran." /> </Target> Another option (machine wide) is to add the following binding redirect to sgen.exe.config: <runtime> <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> <dependentAssembly> <assemblyIdentity name="System.IO.Compression.ZipFile" publicKeyToken="b77a5c561934e089" culture="neutral" /> <bindingRedirect oldVersion="0.0.0.0-4.2.0.0" newVersion="4.0.0.0" /> </dependentAssembly> </assemblyBinding> </runtime> This will only work on machines with .NET Framework 4.7.1. installed. Once .NET Framework 4.7.2 is installed on that machine, this workaround should be removed.

我有同样的问题,唯一的方法,我如何能够修复它是添加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"

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