我想在我的WebAPI解決方案中使用Unity,並試圖從web.config文件加載配置。WebAPI,Unity和Web.config
這是在web.config中團結節:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<assembly name="MyWebAPI" />
<namespace name="MyWebAPI.Controllers" />
<container>
<register type="MyController" mapTo="MyController">
<constructor>
<param name="param1">
<value value="val1" />
</param>
<param name="param2">
<value value="val2" />
</param>
<param name="param3">
<value value="val3" />
</param>
</constructor>
<lifetime type="singleton" />
</register>
</container>
</unity>
的Bootstrapper.cs:
public static IUnityContainer Initialise()
{
var container = BuildUnityContainer();
DependencyResolver.SetResolver(new UnityDependencyResolver(container));
return container;
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer();
// register all your components with the container here
// it is NOT necessary to register your controllers
// e.g. container.RegisterType<ITestService, TestService>();
RegisterTypes(container);
return container;
}
public static void RegisterTypes(IUnityContainer container)
{
container.RegisterType<MyController>();
container.LoadConfiguration();
}
在Global.asax:
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
Bootstrapper.Initialise();
}
但這不是工作我作爲參數沒有進入構造函數。
的的WebAPI控制器構造:
public MyController(string param1, string param2, string param3)
{
_param1 = param1;
_param2 = param2;
_param3 = param3;
}
我試過...我將代碼改爲GlobalConfiguration.Configuration.DependencyResolver = new Unity.WebApi.UnityDependencyResolver(container);但現在我收到此錯誤無法加載文件或程序集'System.Web.Http,版本= 4.0.0.0,文化=中立,PublicKeyToken = 31bf3856ad364e35'或其依賴項之一。定位的程序集清單定義與程序集引用不匹配。 (來自HRESULT的異常:0x80131040) – Looking4Help