无法加载文件或程序集Autofac,版本=3.3.0.0。

15
在将我的项目从Autofac 2.6.3.862升级到3.4.0.0后,我遇到了以下错误。 在解决方案中的任何项目中,甚至没有添加对Autofac 3.3.0.0的引用。
=== Pre-bind state information ===
LOG: DisplayName = Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da
 (Fully-specified)
LOG: Appbase = file:///C:/Projects/Drive/temp/drive/Src/Web/
LOG: Initial PrivatePath = C:\Projects\Drive\temp\drive\Src\Web\bin
Calling assembly : Autofac.Configuration, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\Projects\Drive\temp\drive\Src\Web\web.config
LOG: Using host configuration file: C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet.config
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework\v4.0.30319\config\machine.config.
LOG: Post-policy reference: Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da
LOG: Attempting download of new URL file:///c:/temp/root/79371609/925ee10/Autofac.DLL.
LOG: Attempting download of new URL file:///c:/temp/root/79371609/925ee10/Autofac/Autofac.DLL.
LOG: Attempting download of new URL file:///C:/Projects/Drive/temp/drive/Src/Web/bin/Autofac.DLL.
WRN: Comparing the assembly name resulted in the mismatch: Minor Version
ERR: Failed to complete setup of assembly (hr = 0x80131040). Probing terminated.

Stack Trace: 


[FileLoadException: Could not load file or assembly 'Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   Fairfax.Classifieds.Drive.Web.Global.RegisterTypesWithAutofac() in c:\Projects\Drive\temp\drive\Src\Web\Global.asax.cs:375
   Fairfax.Classifieds.Drive.Web.Global.Application_Start(Object sender, EventArgs e) in c:\Projects\Drive\temp\drive\Src\Web\Global.asax.cs:102

[HttpException (0x80004005): Could not load file or assembly 'Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9936485
   System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
   System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
   System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
   System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296

[HttpException (0x80004005): Could not load file or assembly 'Autofac, Version=3.3.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)]
   System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9950728
   System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
   System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254

更新: 我还在web.config中添加了程序集重定向绑定,但仍然出现错误。

<runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="System.Web.Mvc" publicKeyToken="31bf3856ad364e35" />
                <bindingRedirect oldVersion="1.0.0.0-3.0.0.0" newVersion="5.2.2.0" />
            </dependentAssembly>
    <dependentAssembly>
                <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" />
                <bindingRedirect oldVersion="1.0.0.0-3.4.0.0" newVersion="3.4.0.0" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>

我也尝试将Autofac 3.3.0.0添加到GAC中,但是没有起作用。


我最终将 Microsoft.AspNet.Mvc 降级到了 5.1.0 版本,Autofac 也降级到了 3.4.0 版本。 - Jin Ho
我添加WCF引用时遇到了类似的错误。看起来Autofac.Integration.Wcf需要Autofac 3.3,但我有Autofac 3.5。下面建议的绑定重定向已经存在,但没有帮助。它在编译时有帮助,因为我可以在构建日志中看到,但在添加服务引用时没有帮助。“无法加载文件或程序集“Autofac,Version=3.3.0.0,Culture=neutral,PublicKeyToken=17863af14b0044da”或其某个依赖项。系统找不到指定的文件。” - Kevin Kalitowski
我曾经遇到过同样的问题,但是在修改Web.config后进行了一次清理和重建就解决了。 - cederlof
3个回答

23

你可能需要在web.config文件中添加一个绑定重定向。我的版本是3.5,看起来像这样:

  <dependentAssembly>
    <assemblyIdentity name="Autofac" publicKeyToken="17863af14b0044da" culture="neutral" />
    <bindingRedirect oldVersion="0.0.0.0-3.5.0.0" newVersion="3.5.0.0" />
  </dependentAssembly>

1
不要忘记在 newVersion 上使用正确的值。在 Visual Studio 中,打开解决方案资源管理器,选择“引用”,点击 Autofac,查看属性窗口中的 Version 值。在 web.config 中的 newVersion 上,使用在属性窗口中找到的值。 - Roberto Correia
在我的情况下,引用 Autofac.WebApi2 nuget 并设置 DependencyResolver 导致了 OP 所遇到的问题。绑定重定向有助于解决它。 - ArcX

0

0

我曾经遇到过一个问题,与一个单元测试库有关,我将其从旧的XAML构建迁移到了新的vNext构建。我为所有使用AutoFac的库配置了程序集版本绑定重定向。但最终输出结果仍然存在问题。

我最终发现,绑定重定向信息只在与项目相应的DLL文件(位于bin文件夹中)中输出,而不是所有其他包含的DLL文件。在我的情况下,我引用了另一个(单元测试)库,这个库也需要AutoFac,其中包含了也被构建进程检测到并在运行时失败的单元测试。问题在于,被引用库中定义的绑定重定向没有被应用,因为被引用库的.config文件不存在于bin文件夹中。

所以,我通过不让单元测试库引用其他单元测试库来解决了这个问题。可以通过使用公共代码项目或公共C#项目库来解决问题。

关于为什么需要自动工厂程序集绑定的其他信息,请参见此处: 为什么不是所有Autofac软件包都针对最新的Autofac核心?


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接