简体   繁体   English

无法加载文件或程序集“System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”

[英]Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"

I've copied my project to a clean Windows 10 machine with only Visual Studio 2015 Community and SQL Server 2016 Express installed.我已将我的项目复制到一台干净的 Windows 10 机器上,只安装了 Visual Studio 2015 Community 和 SQL Server 2016 Express。 There are no other framework versions installed apart from those installed with Windows 10 and VS2015 or SQL Server.除了Windows 10和VS2015或SQL Server安装的框架版本外,没有安装其他框架版本。

When I try to start the WebApi project I get the message:当我尝试启动 WebApi 项目时,我收到消息:

Could not load file or assembly "System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" or one of its dependencies.无法加载文件或程序集“System.Net.Http,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一。 The system cannot find the file specified.该系统找不到指定的文件。

The project's packages include:该项目的包包括:

<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" />

After building the project with .NET Framework 4.6.1, System.Net.Http the file is not found in the bin folder.使用 .NET Framework 4.6.1、 System.Net.Http构建项目后,在bin文件夹中找不到该文件。

The file's path points to:该文件的路径指向:

C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework.NETFramework\v4.6.1\System.Net.Http.dll

The file's path of System.Net.Http.Formatting points to: System.Net.Http.Formatting的文件路径指向:

C:\Development\MyApp\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib.net45\System.Net.Http.Formatting.dll C:\Development\MyApp\packages\Microsoft.AspNet.WebApi.Client.5.2.3\lib.net45\System.Net.Http.Formatting.dll

Should the whole project target 4.5.1 or is there another way to reference the right assemblies?整个项目应该以 4.5.1 为目标还是有其他方法来引用正确的程序集?

Changing the binding information in my web.config (or app.config) - while a "hack" in my view, allows you to move forward with your project after a NuGet package update whacks your application and gives you the System.Net.Http error.更改我的 web.config(或 app.config)中的绑定信息 - 虽然在我看来是“黑客”,但允许您在 NuGet 包更新破坏您的应用程序并为您提供System.Net.Http后继续您的项目错误。

Set oldVersion="0.0.0.0-4.1.1.0" and newVersion="4.0.0.0" as follows设置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>

Follow the following steps:请按照以下步骤操作:

  1. Update Visual Studio to to latest version (it matters)将 Visual Studio 更新到最新版本(很重要)

  2. Remove all binding redirects from web.configweb.config中删除所有绑定重定向

  3. Add this to the .csproj file:将此添加到.csproj文件中:

     <PropertyGroup> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> <GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType> </PropertyGroup>
  4. Build the project构建项目

  5. In the bin folder there should be a (WebAppName).dll.config filebin文件夹中应该有一个(WebAppName).dll.config文件

  6. It should have redirects in it, copy these to the web.config它应该有重定向,将它们复制到web.config

  7. Remove the above snipped from the .csproj file.csproj文件中删除上面的片段

It should work.它应该工作。

In one of my projects there was a nuget packages with higher version of System.Net.Http.在我的一个项目中,有一个具有更高版本 System.Net.Http 的 nuget 包。 and in my startup project there reference to System.Net.Http v 4.0.0 , i just installed System.Net.Http nuget package in my startup project and the problem solved并且在我的启动项目中有对 System.Net.Http v 4.0.0 的引用,我刚刚在我的启动项目中安装了System.Net.Http nuget 包并且问题解决了

Change following:更改以下内容:

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

with the following:具有以下内容:

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

in web.config在 web.config 中

The above bind-redirect did not work for me so I commented out the reference to System.Net.Http in web.config .上面的绑定重定向对我不起作用,所以我注释掉了web.config中对System.Net.Http的引用。 Everything seems to work OK without it.没有它,一切似乎都可以正常工作。

  <system.web>
    <compilation debug="true" targetFramework="4.7.2">
      <assemblies>
        <!--<add assembly="System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />-->
        <add assembly="System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=B77A5C561934E089" />
      </assemblies>
    </compilation>
    <customErrors mode="Off" />
    <httpRuntime targetFramework="4.7.2" />
  </system.web>

If you have multiple projects in your solution, then right-click on the solution icon in Visual Studio and select 'Manage NuGet Packages for Solution', then click on the fourth tab 'Consolidate' to consolidate all your projects to the same version of the DLLs.如果您的解决方案中有多个项目,则右键单击 Visual Studio 中的解决方案图标并选择“管理解决方案的 NuGet 包”,然后单击第四个选项卡“合并”以将所有项目合并到相同版本的DLL。 This will give you a list of referenced assemblies to consolidate.这将为您提供要合并的引用程序集列表。 Click on each item in the list, then click install in the tab that appears to the right.单击列表中的每个项目,然后单击右侧显示的选项卡中的安装。

This will work in .NET 4.7.2 with Visual Studio 2017 (15.9.4):这将适用于带有 Visual Studio 2017 (15.9.4) 的 .NET 4.7.2:

  • Remove web/app.config binding redirects删除 web/app.config 绑定重定向
  • Remove NuGet package for System.Net.Http删除 System.Net.Http 的 NuGet 包
  • Open "Add New Reference" and directly link to the new 4.2.0.0 build that ships with .NET 4.7.2打开“添加新引用”并直接链接到 .NET 4.7.2 附带的新 4.2.0.0 构建

![图片](https://user-images.githubusercontent.com/38843378/50998531-b5bb3a00-14f5-11e9-92df-6c590c469349.png)

You can fix this by upgrading your project to .NET Framework 4.7.2.您可以通过将项目升级到 .NET Framework 4.7.2 来解决此问题。 This was answered by Alex Ghiondea - MSFT . Alex Ghiondea-MSFT 回答了这个问题。 Please go upvote him as he truly deserves it!请给他点赞,因为他真的应得的!

This is documented as a known issue in .NET Framework 4.7.1.这被记录为 .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)他们将从传递给 SGEN 的引用列表中删除 DesignFacadesToFilter(并在 SGEN 完成后将它们添加回来)

 <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:另一个选项(机器范围)是将以下绑定重定向添加到 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.

4.6.1-2 in VS2017 users may experience the unwanted replacement of their version of System.Net.Http by the one VS2017 or Msbuild 15 wants to use. VS2017 中的 4.6.1-2 用户可能会遇到不需要的 System.Net.Http 版本被 VS2017 或 Msbuild 15 想要使用的替换。

We deleted this version here:我们在这里删除了这个版本:

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\Professional\MSBuild\Microsoft\Microsoft.NET.Build.Extensions\net461\lib\System.Net.Http.dll

and here:和这里:

C:\Program Files (x86)\Microsoft Visual Studio\2017\BuildTools\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

Then the project builds with the version we have referenced via NuGet.然后使用我们通过 NuGet 引用的版本构建项目。

I have same issue and only way how i am able to fix it is add bindingRedirect to app.confing how wrote @tripletdad99.我有同样的问题,我能够解决它的唯一方法是将 bindingRedirect 添加到 app.confing 如何写@tripletdad99。

But if you have solution with more project is really suck update every project by hand (and also sometimes after update some nuget package you need to do it again).但是,如果你有更多项目的解决方案真的很糟糕手动更新每个项目(有时在更新一些 nuget 包后你需要再做一次)。 And it is reason why i wrote simple powershell script which if all app.configs.这就是我编写简单的 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"

Example how to use:示例如何使用:

./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"

Probably it is not perfect and also it will be better if somebody link it to pre-build task.可能它并不完美,如果有人将它链接到预构建任务会更好。

I had this, but, it was because I had added a NuGet package that had updated the binding redirects.我有这个,但是,这是因为我添加了一个更新绑定重定向的 NuGet 包。 Once I removed the package, the redirects were still there.一旦我删除了包,重定向仍然存在。 I removed all of them, and then ran update-package -reinstall.我删除了所有这些,然后运行 ​​update-package -reinstall。 This added the correct redirects.这添加了正确的重定向。

Before doing tricks and configuration complexities, try deleting the bin and obj folders, then compile.在进行技巧和配置复杂性之前,请尝试删除 bin 和 obj 文件夹,然后编译。 That fixed same problem解决了同样的问题

Check .net framework version.检查 .net 框架版本。
My original .net framework is older version.我原来的 .net 框架是旧版本。
After I installed .net framework 4.6, this issue is automatically solved.安装 .net framework 4.6 后,这个问题就自动解决了。

For me, I had set my project to run on the latest version of .Net Framework (a change from .Net Framework 4.6.1 to 4.7.2).对我来说,我已将我的项目设置为在最新版本的 .Net Framework 上运行(从 .Net Framework 4.6.1 更改为 4.7.2)。

Everything worked, no errors and published without issue, and it was only by chance that I came across the System.Net.Http error message, shown in a small, hard-to-notice, but quite important API request over the website I'm working on.一切正常,没有错误,发布也没有问题,我偶然发现 System.Net.Http 错误消息,显示在网站上的一个很小的、难以注意到但非常重要的 API 请求中米工作。

I rolled back to 4.6.1 and everything is fine again.我回滚到 4.6.1,一切又好了。

The only way that cleanly solved this issue for me (.NET 4.6.1) was to not only add a Nuget reference to System.Net.Http V4.3.4 for the project that actually used System.Net.Http, but also to the startup project (a test project in my case).为我 (.NET 4.6.1) 彻底解决此问题的唯一方法是不仅为实际使用 System.Net.Http 的项目添加对 System.Net.Http V4.3.4 的 Nuget 引用,而且还添加到启动项目(在我的例子中是一个测试项目)。

(Which is strange, because the correct System.Net.Http.dll existed in the bin directory of the test project and the .config assemblyBingings looked OK, too.) (这很奇怪,因为正确的 System.Net.Http.dll 存在于测试项目的 bin 目录中,并且 .config assemblyBingings 看起来也不错。)

Was updating an old website using nuget (including .Net update and MVC update).正在使用 nuget 更新旧网站(包括 .Net 更新和 MVC 更新)。

I deleted the System.Net.HTTP reference in VS2017 (it was to version 2.0.0.0) and re-added the reference, which then showed 4.2.0.0.我删除了 VS2017 中的 System.Net.HTTP 引用(它是版本 2.0.0.0)并重新添加了引用,然后显示 4.2.0.0。

I then updated a ton of 'packages' using nuget and got the error message, then noticed something had reset the reference to 2.0.0.0, so I removed and re-added again and it works fine... bizarre.然后我使用 nuget 更新了大量的“包”并收到错误消息,然后注意到某些东西已将引用重置为 2.0.0.0,所以我删除并再次重新添加它工作正常......奇怪。

对我来说,我再次添加了nuget,问题就解决了

Removing dependentAssembly for name="System.Net.Http" from web.config also worked for me.从 web.config 中删除 name="System.Net.Http" 的dependentAssembly 也对我有用。 I commented this part from web.config and it worked for me.我从 web.config 评论了这部分,它对我有用。 If all the above solutions didn't worked for you try commenting or removing as show below.如果上述所有解决方案都不适合您,请尝试如下所示进行评论或删除。

<!--<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>-->
  1. Open Web.config and remove < runtime > including everything inside it打开 Web.config 并删除 < runtime > 包括其中的所有内容

    <**runtime>< assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> </runtime > <**runtime>< assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1"> </runtime >

  2. Save and close Web.config保存并关闭 Web.config

  3. Open Package Manager Console and run the command打开包管理器控制台并运行命令

    Add-BindingRedirect添加绑定重定向

This solved my problem when my team member upgraded from .NET Framework 4.6.1 to .NET Framework 4.8 and VS 2017 to VS 2022当我的团队成员从 .NET Framework 4.6.1 升级到 .NET Framework 4.8 和 VS 2017 到 VS 2022 时,这解决了我的问题

I had this issue after upgrading the project from .NET Framework 4.6 to .NET Framework 4.8.将项目从 .NET Framework 4.6 升级到 .NET Framework 4.8 后,我遇到了这个问题。 In the config file I still had the line with the old version在配置文件中,我仍然有旧版本的行

<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1"/>

and the server didn't have .NET Framework 4.8 installed.并且服务器没有安装 .NET Framework 4.8。

After correcting the line above, installing .NET Framework 4.8 on the server and rebooting it, now it works.更正上述行后,在服务器上安装 .NET Framework 4.8 并重新启动它,现在它可以工作了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 未找到此程序集:“System.Net.Http,版本=4.0.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a” - This assembly is not found: 'System.Net.Http, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Azure应用服务 - 无法加载文件或程序集&#39;System.Net.Http,Version = 4.2.0.0,Culture = neutral,PublicKeyToken = b03f5f7f11d50a3a&#39; - Azure app service - Could not load file or assembly 'System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 无法加载文件或程序集“System.IO,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一 - Could not load file or assembly 'System.IO, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies 无法加载文件或程序集“ System.Configuration,版本= 4.0.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a”或其依赖项之一 - Could not load file or assembly 'System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies 无法加载文件或程序集“System.Runtime,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a”或其依赖项之一 - Could not load file or assembly 'System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies NET 核心 - 无法加载文件或程序集'System.Runtime,版本 = 6.0.0.0,文化 = 中性,PublicKeyToken = b03f5f7f11d50a3a',.NET 6 - NET core - Could not load file or assembly 'System.Runtime, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a ', .NET 6 &#39;无法加载文件或程序集&#39;System.Runtime,版本= 4.1.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a&#39; - 'Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' linqpad无法加载文件或程序集&#39;System.Runtime,版本= 4.1.0.0,区域性=中性,PublicKeyToken = b03f5f7f11d50a3a&#39; - linqpad Could not load file or assembly 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 无法加载文件或程序集“System.Runtime,版本=4.2.1.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a” - Could not load file or assembly 'System.Runtime, Version=4.2.1.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' 无法从程序集“System.Web,版本=4.0.0.0,Cult=neutral,PublicKeyToken=b03f5f7f11d50a3a”加载类型“System.Web.UI.ParseChildrenAttribute” - Could not load type 'System.Web.UI.ParseChildrenAttribute' from assembly 'System.Web, Version=4.0.0.0, Cult=neutral, PublicKeyToken=b03f5f7f11d50a3a'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM