そのため、 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 実装に関する実際の例や問題へのコメントがあれば、喜んでそれを見ていきます。