使用IoC容器将依赖项注入ASP.NET WebAPI控制器中,是否有人能成功运行?我似乎无法使其正常工作。
这就是我现在正在做的。
在我的global.ascx.cs
:
public static void RegisterRoutes(RouteCollection routes)
{
// code intentionally omitted
}
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
IUnityContainer container = BuildUnityContainer();
System.Web.Http.GlobalConfiguration.Configuration.ServiceResolver.SetResolver(
t =>
{
try
{
return container.Resolve(t);
}
catch (ResolutionFailedException)
{
return null;
}
},
t =>
{
try
{
return container.ResolveAll(t);
}
catch (ResolutionFailedException)
{
return new System.Collections.Generic.List<object>();
}
});
System.Web.Mvc.ControllerBuilder.Current.SetControllerFactory(new UnityControllerFactory(container));
BundleTable.Bundles.RegisterTemplateBundles();
}
private static IUnityContainer BuildUnityContainer()
{
var container = new UnityContainer().LoadConfiguration();
return container;
}
我的控制器工厂:
public class UnityControllerFactory : DefaultControllerFactory
{
private IUnityContainer _container;
public UnityControllerFactory(IUnityContainer container)
{
_container = container;
}
public override IController CreateController(System.Web.Routing.RequestContext requestContext,
string controllerName)
{
Type controllerType = base.GetControllerType(requestContext, controllerName);
return (IController)_container.Resolve(controllerType);
}
}
它似乎从未在我的统一文件中查找来解决依赖关系,并且出现如下错误:
尝试创建类型为'PersonalShopper.Services.WebApi.Controllers.ShoppingListController'的控制器时发生错误。确保控制器具有无参数的公共构造函数。
在System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateInstance(在System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateController)处(System.Web.Http.Dispatcher.DefaultHttpControllerFactory.CreateInstance(在HttpControllerContext controllerContext,HttpControllerDescriptor controllerDescriptor) System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncInternal(HttpRequestMessage请求,CancellationToken cancelToken)处的(HttpControllerContext controllerContext,String controllerName)在System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsync(HttpRequestMessage请求,CancellationToken cancelToken)处的(HttpControllerContext controllerContext,String controllerName)
控制器看起来像:
public class ShoppingListController : System.Web.Http.ApiController
{
private Repositories.IProductListRepository _ProductListRepository;
public ShoppingListController(Repositories.IUserRepository userRepository,
Repositories.IProductListRepository productListRepository)
{
_ProductListRepository = productListRepository;
}
}
我的统一文件如下所示:
<unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
<container>
<register type="PersonalShopper.Repositories.IProductListRepository, PersonalShopper.Repositories" mapTo="PersonalShopper.Implementations.MongoRepositories.ProductListRepository, PersonalShopper.Implementations" />
</container>
</unity>
请注意,我没有控制器本身的注册,因为在早期版本的mvc中,控制器工厂会指出需要解决依赖关系。
似乎从未调用过我的控制器工厂。
GlobalConfiguration.Configuration.ServiceResolver.SetResolver()
?