因此,我使用Mark Seemann 的示例在 MVC 4 RC Web Api 中使用 Windsor 进行依赖注入,但我收到异常说它无法解析对我的 ApiController 的依赖
public class StatisticsController : ApiController
{
private readonly ILogger _logger;
private readonly IClickMessageProducer _producer;
public StatisticsController(ILogger logger,
IClickMessageProducer clickMsgProducer)
{
_logger = logger;
_producer = clickMsgProducer;
}
public string Get(string msg, string con) {...}
}
我的 Global.asax 看起来像这样:
protected void Application_Start()
{
// different configs removed for brevity
BootstrapContainer();
}
private static IWindsorContainer _container;
private static void BootstrapContainer()
{
_container = new WindsorContainer()
.Install(FromAssembly.This(), new ProducerInstaller())
.Install(FromAssembly.This(), new WebWindsorInstaller());
GlobalConfiguration.Configuration.Services.Replace(
typeof(IHttpControllerActivator),
new WindsorHttpControllerActivator(_container));
}
安装人员为 Windsor 提供了所需的参考资料IClickMessageProducer
。我IController
在一个真正的 MVC 4 项目中使用它,所以我相信这部分是有效的。
要指定,这是我在尝试通过StatisticsController
对 API 的 GET 调用访问方法时收到的错误消息:
<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
Can't create component 'APIMVC.Controllers.StatisticsController'
as it has dependencies to be satisfied.
'APIMVC.Controllers.StatisticsController' is waiting for the following
dependencies: - Service 'Castle.Core.Logging.ILogger' which was not registered.
</ExceptionMessage>
<ExceptionType>Castle.MicroKernel.Handlers.HandlerException</ExceptionType>
<StackTrace>...</StackTrace>
</Error>
调用是这样的:“http://localhost:60000/api/statistics?msg=apitest&con=apimvc”
如果有人有一个工作示例或只是对我的 Windsor 实施问题的评论,我会很高兴看到它。