我无法使用 asp.net web api 配置 Windsor Container,尽管它与常规控制器(IController)一起工作正常。
我在同一个项目中有常规控制器和 api 控制器。
这是正常控制器的代码:
/// <summary>
/// Controller Factory class for instantiating controllers using the Windsor IoC container.
/// </summary>
public class WindsorControllerActivator : IControllerActivator
{
private readonly IWindsorContainer container;
public WindsorControllerActivator(IWindsorContainer container)
{
this.container = container;
}
public IController Create(RequestContext requestContext, Type controllerType)
{
var controller = (IController)container.GetService(controllerType);
return controller;
}
}
在 global.asax.cs 中
container.Register(Component.For<IWindsorContainer>().Instance(container));
container.Register(Component.For<IControllerActivator>().ImplementedBy<WindsorControllerActivator>());
以下是有问题的代码:
public class WindsorApiControllerActivator : IHttpControllerActivator
{
private readonly IWindsorContainer container;
public WindsorApiControllerActivator(IWindsorContainer container)
{
this.container = container;
}
public IHttpController Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
{
var controller = (IHttpController)container.GetService(controllerType);
return controller;
}
}
和安装程序:
container.Register(Component.For<IHttpControllerActivator>().ImplementedBy<WindsorApiControllerActivator>());
但是,我永远无法执行 WindsorApiControllerActivator。
什么时候,我将调试器放在 WindsorControllerActivator 中,它按预期工作,但对于 WindsorApiControllerActivator,它永远不会被执行。
您的帮助将不胜感激