Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question
 
How do you register your container as the dependencyresolver? –  nemesv Sep 24 '12 at 4:46
 
Why you customize WindsorApiControllerActivator whereas I don't see any special treatment? –  Cuong Le Sep 24 '12 at 4:59
 
Cuong le: because i using Nhibernate Session object in the api controllers <br /> public ISession NSession { get; set; } public VendorController(ISession session) { NSession = session; } // GET api/<controller> [HttpPost] public IList<SearchResultViewModel> Search(SearchViewModel model) { IList<SearchResultViewModel> searchResultViewModels = SearchVenues(model); return searchResultViewModels; //return "Hello World"; } –  aamir sajjad Sep 24 '12 at 5:34
 
@nemesv:Any Update from your side –  aamir sajjad Sep 24 '12 at 9:59
 
@CuongLe: Any Update? –  aamir sajjad Sep 24 '12 at 9:59
show 4 more comments

3 Answers

You should be very carefull to use an IDependencyResolver for Windosr w/o decomissiong/release concern.

Have a look to this implementation http://nikosbaxevanis.com/2012/06/04/using-the-web-api-dependency-resolver-with-castle-windsor-part-2/ I didn't try it yet, but it seems the way to go w/ current webapi release

share|improve this answer
add comment
up vote 0 down vote accepted

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);
share|improve this answer
add comment

Take a look at the solution proposed by Mark Seemann in this article: http://blog.ploeh.dk/2012/10/03/DependencyInjectionInASPNETWebAPIWithCastleWindsor.aspx

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.