我正在运行集成测试,当我到达这行代码时:
WebApiDependencyResolverConfig.Register(config);
(在内部使用autofac容器)
我遇到了这个异常:
{"Could not load file or assembly 'System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)":"System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"}
Fusionlog:
=== Pre-bind state information ===
LOG: DisplayName = System.Web.Http, Version=5.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35
(Fully-specified)
LOG: Appbase = file:///C:/TLP/TLP.API.IntegrationTests/bin/Debug
LOG: Initial PrivatePath = NULL
Calling assembly : Autofac.Integration.WebApi, Version=3.0.0.0, Culture=neutral, PublicKeyToken=17863af14b0044da.
===
LOG: This bind starts in default load context.
LOG: Using application configuration file: C:\TLP\TLP.API.IntegrationTests\bin\Debug\TLP.API.IntegrationTests.dll.config
LOG: Using host configuration file:
LOG: Using machine configuration file from C:\Windows\Microsoft.NET\Framework64\v4.0.30319\config
似乎autofac web api集成仅适用于web api 2.0。当我使用不再引用system.web.http 5.0.0而是改为5.1.0的web api 2.1时,它就无法工作了。 我应该如何告诉autofac使用system.web.http 5.1.0版本而不是5.0.0? 我在我的集成测试和API项目的app.config中添加了以下内容:
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="System.Web.Http"
publicKeyToken="32ab4ba45e0a69a1"
culture="neutral" />
<bindingRedirect oldVersion="5.0.0.0"
newVersion="5.1.0.0"/>
</dependentAssembly>
</assemblyBinding>
</runtime>
但是它并没有起作用!
另一个非常奇怪的事情是,我读过使用.NET 4.5.1时,这个重定向程序集的东西会自动完成。但是它并没有发生...