C# 简单注入器依赖项解析错误-无法加载文件或程序集System.Web.Http
我遵循洋葱架构,并在DependencyResolution项目中使用C# 简单注入器依赖项解析错误-无法加载文件或程序集System.Web.Http,c#,.net,asp.net-web-api,dependency-injection,simple-injector,C#,.net,Asp.net Web Api,Dependency Injection,Simple Injector,我遵循洋葱架构,并在DependencyResolution项目中使用简单注入器。以下是我的架构: 1-Core - Domain Classes - Repository Interfaces - Service Interfaces 2-Infrastructure - Data - Dependency Resolution - Repository Interfaces Implementation - Service
简单注入器
。以下是我的架构:
1-Core
- Domain Classes
- Repository Interfaces
- Service Interfaces
2-Infrastructure
- Data
- Dependency Resolution
- Repository Interfaces Implementation
- Service Interfaces Implementation
3-WebApi
- Web Api Project
4-WebClient
- My AngularJs App
5-Test
- Test Project
StartUp.cs
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
// Here I am getting error at runtime.
SimpleInjectorInitializer.Initialize(app);
ConfigureAuth(app);
}
}
SimpleInjectorInitializer.Initialize
public static Container Initialize(IAppBuilder app)
{
var container = GetInitializeContainer(app);
// This is an extension method from the integration package.
container.RegisterWebApiControllers(GlobalConfiguration.Configuration);
container.Verify();
GlobalConfiguration.Configuration.DependencyResolver =
new SimpleInjectorWebApiDependencyResolver(container);
return container;
}
在这里,我添加了对取自WebApi项目的System.Web、System.Web.Http、System.Web.Http.Host的引用
我将DependencyResolution项目的输出路径放入WebApi bin,并在web.config
中设置owinstart的值
以下是我得到的错误:
中发生类型为“System.IO.FileLoadException”的异常
Infrastructure.DependencyResolution.dll,但未在用户中处理
代码
其他信息:无法加载文件或程序集
'System.Web.Http,版本=4.0.0.0,区域性=中性,
PublicKeyToken=31bf3856ad364e35'或其依赖项之一。这个
定位程序集的清单定义与程序集不匹配
参考资料。(来自HRESULT的异常:0x8013100)
编辑:
下面是App.Config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
</configSections>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="Microsoft.Owin.Security.Cookies" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-3.0.1.0" newVersion="3.0.1.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Net.Http.Formatting" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
<dependentAssembly>
<assemblyIdentity name="System.Web.Http" publicKeyToken="31bf3856ad364e35" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.2.3.0" newVersion="5.2.3.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>
应用程序配置文件中可能缺少绑定重定向。在大多数情况下,NuGet包管理器将为您正确设置绑定重定向,因为有时它无法做到这一点
配置文件中需要的绑定重定向如下所示:
请注意,您需要为所引用的实际版本替换{version}
占位符。您确定在使用依赖项注入的整个项目中使用了相同版本的SimpleInjector吗?我遇到了类似的错误,我注意到SimpleInjectors旧版本在一个项目中使用。在我用nuget将SimpleInjector更新为新版本后,它开始工作。在我的例子中,SimpleInjector正确地添加了System.Web.Http的程序集标识-我必须删除bin
和obj
文件夹,以便它开始使用最新的Web.config.Dude,你救了我的命!我要向你竖起十个大拇指!