I am unable to configure Windsor Container with asp.net web api, though it working fine with regular controller(IController).
I have regular controller and api controller in same project.
Here is the code for regular controller which working fine:
/// <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;
}
}
and in global.asax.cs
container.Register(Component.For<IWindsorContainer>().Instance(container));
container.Register(Component.For<IControllerActivator>().ImplementedBy<WindsorControllerActivator>());
and below is the problematic code:
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;
}
}
and installer:
container.Register(Component.For<IHttpControllerActivator>().ImplementedBy<WindsorApiControllerActivator>());
however, i am never able to execute WindsorApiControllerActivator.
when, i put debugger in WindsorControllerActivator it work as expected, but for WindsorApiControllerActivator it never get executed.
your help will be appreciated
I added the following code in global.asax.cs, and it works:)
_windsorContainer = new WindsorContainer();
_windsorContainer.Install(new EventSorbetInstaller());
DependencyResolver.SetResolver(new WindsorDependencyResolver(_windsorContainer));
ServiceLocator.SetLocatorProvider(() => new WindsorServiceLocator(_windsorContainer));
RegisterFilterProviders(FilterProviders.Providers, _windsorContainer);
var activator = _windsorContainer.Resolve<IHttpControllerActivator>();
GlobalConfiguration.Configuration.Services.Replace(typeof(IHttpControllerActivator), activator);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With