5

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

aamir sajjad
  • 3,019
  • 1
  • 27
  • 26
  • How do you register your container as the dependencyresolver? – nemesv Sep 24 '12 at 04:46
  • Why you customize WindsorApiControllerActivator whereas I don't see any special treatment? – cuongle Sep 24 '12 at 04:59
  • Cuong le: because i using Nhibernate Session object in the api controllers
    public ISession NSession { get; set; } public VendorController(ISession session) { NSession = session; } // GET api/ [HttpPost] public IList Search(SearchViewModel model) { IList searchResultViewModels = SearchVenues(model); return searchResultViewModels; //return "Hello World"; }
    – aamir sajjad Sep 24 '12 at 05:34
  • @nemesv:Any Update from your side – aamir sajjad Sep 24 '12 at 09:59
  • Don't inject the Windsor container into your components: http://stackoverflow.com/questions/4985455/dependency-injection-vs-service-location. – Steven Sep 24 '12 at 12:04
  • @Steven: actually it is brown field application, therefore no other choice then using it:) – aamir sajjad Sep 24 '12 at 13:48
  • @aamirsajjad: I'm sorry, your `WindsorControllerActivator` is probably part of your Composition Root. In that case it's fine to do so. – Steven Sep 24 '12 at 14:10
  • See http://stackoverflow.com/a/19613137/114029 – Leniel Maccaferri Oct 26 '13 at 23:46

2 Answers2

1

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);
aamir sajjad
  • 3,019
  • 1
  • 27
  • 26
0

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

Crixo
  • 3,060
  • 1
  • 24
  • 32