首页 > 解决方案 > 无法加载文件或程序集 System.Net.Http 4.2.0.0

问题描述

我真的坚持这一点,可以使用一些帮助。

当我将涉及 httpclient 的异步方法添加到类库时,这个问题就开始了。我从一个简短的测试控制台应用程序调用类库。当它到达异步方法时,我得到一个异常:

System.IO.FileNotFoundException:'无法加载文件或程序集'System.Net.Http,版本 = 4.2.0.0,文化 = 中性,PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。该系统找不到指定的文件。'

最初它说无法加载 System.Net.Http 版本 4.1.1.2(我在项目中有版本 4.0.0.0)并且我使用的是 Framework 4.5.2。我找不到适用于 NET Framework 4.5.2 的 system.net.http 版本 4.1.1.2,所以我首先将类库和测试项目更新到 NFW 4.7.2,然后添加对 System.Net.Http 相关版本的引用使用此框架(即从程序集选项卡中选择它)。

当我查看类库项目(toolslib)中 dll 的属性时,它显示 4.2.0.0。我向用于调用它的测试项目添加了相同的引用,它现在也显示 4.2.0.0。

此外,我在工具库的 app.config 中添加了一个重定向部分

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

我在测试控制台应用程序的 app.config 中放置了相同的行。

但是我现在仍然遇到同样的问题,它说它无法加载 4.2.0.0。我设置了 Fusion Logs 并在运行并获得异常后得到以下信息:

*** Assembly Binder Log Entry  (7/9/2020 @ 9:12:52 PM) ***

The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework\v4.0.30319\clr.dll
Running under executable  C:\Users\smjhu\Documents\DevProjects\IDXUtil\Test\bin\Debug\Test.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http, Version=4.2.0.0, Culture=neutral,  ]PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL
LOG: AppName = Test.exe
Calling assembly : ToolsLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Users\smjhu\Documents\DevProjects\IDXUtil\Test\bin\Debug\Test.exe.Config
LOG: Using host configuration file: 
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Redirect found in application configuration file: 4.2.0.0 redirected to 4.2.0.0.
LOG: Post-policy reference: System.Net.Http, Version=4.2.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
LOG: GAC Lookup was unsuccessful.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http/System.Net.Http.DLL.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http.EXE.
LOG: Attempting download of new URL file:///C:/Users/smjhu/Documents/DevProjects/IDXUtil/Test/bin/Debug/System.Net.Http/System.Net.Http.EXE.
LOG: All probing URLs attempted and failed.

我从日志中唯一能看到的是它似乎正在尝试在 test/bin/debug 文件夹中查找 dll。

我尝试将 dll 设置为复制本地但结果相同。

然后我尝试手动将 system.net.http dll 复制到 bin/debug 文件夹中,但是当我点击该方法时,我得到一个 badimage 异常,说我不能使用引用程序集。

顺便说一句,还尝试使用 nuget 更新 system.net.http,结果相同

标签: c#

解决方案


如果我从控制台应用程序中删除 bindingredirect,问题就会消失。

不幸的是,这并不能真正解释问题,但至少现在可以解决它。


推荐阅读