为什么Autofac.Integration.WebApi.Owin 5.0.0在TargetFramework v4.7.1上引用System.Net.Http 4.2.0?

问题描述

我有一个带有TargetFrameworkVersion v4.7.1的程序集,该程序集引用了Autofac.Integration.WebApi.Owin 5.0.0。

在编译过程中,我收到警告说 warning MSB3277: Found conflicts between different versions of "System.Net.Http" that could not be resolved. These reference conflicts are listed in the build log when log verbosity is set to detailed. 在仅安装.NET Framework 4.7.1的服务器上运行我的应用程序,我得到以下运行时异常

System.IO.FileNotFoundException: Could not load file or assembly 'System.Net.Http,Version=4.2.0.0,Culture=neutral,PublicKeyToken=b03f5f7f11d50a3a' or one of its dependencies. The system cannot find the file specified.
File name: 'System.Net.Http,PublicKeyToken=b03f5f7f11d50a3a'
   at Owin.AutofacWebApiAppBuilderExtensions.UseAutofacWebApi(IAppBuilder app,HttpConfiguration configuration)

Assembly manager loaded from:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319\clr.dll
Running under executable  C:\xx\yyServer\yyServer.exe
--- A detailed error log follows. 

=== Pre-bind state information ===
LOG: DisplayName = System.Net.Http,PublicKeyToken=b03f5f7f11d50a3a
 (Fully-specified)
LOG: Appbase = file:///C:/xx/yy/Server/
LOG: Initial PrivatePath = NULL
Calling assembly : Autofac.Integration.WebApi.Owin,Version=5.0.0.0,PublicKeyToken=17863af14b0044da.

在dotPeek中,我可以看到有关程序集的以下信息

Autofac.Integration.WebApi,5.0.0.0,msil,.Net Framework v4.6.1,Debug
   References:
     - System.Net.Http (4.2.0.0)
Autofac.Integration.WebApi.Owin,Debug
   References:
     - System.Net.Http (4.2.0.0)
My.Assembly,1.0.0.0,.Net Framework v4.7.1,Debug
   References:
     - System.Net.Http (4.0.0.0)

如果我针对.NET Framework v.4.6.1-4.7.1创建一个新的空项目,则System.Net.Http参考声称版本为4.0.0.0。如果我将项目提高到4.7.1,则System.Net.Http参考说明为4.2.0.0。查看System.Net.Http的nuget软件包,没有4.2.0版本。在实际项目中使用.NET Framework 4.7.2可能有效,但是目前我无法更新到新的TargetFramework。

将以下程序集重定向添加到app.config似乎可以在运行时解决此特定问题,但是我不确定我对重定向框架程序集的满意程度以及可能带来的其他后果。编译时间警告仍然存在。

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

为什么Autofac组件引用版本4.2.0?包裹有问题吗?是否有比使用程序集重定向更好的方法来解决此问题?

解决方法

将Autofac取出一分钟,如果执行search on system.net.http 4.2.0.0,则会发现从DocumentDbSystem.Collections.Immutable的许多软件包都存在此问题。显然,这源于工具问题,其中的问题是使用VS 2017构建的程序集(这是System.Net.Http 4.2.0.0的来源),并且在构建时存在该版本,但在运行时项目不是 使用相同的工具集构建,而程序集丢失。

如您所见,短期解决方法是程序集绑定重定向。

对于长期解决方法,Autofac软件包需要更新。 I've filed an issue on your behalf.

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...