IoC: Castle Windsor and WebAPI

后端 未结 4 1144
甜味超标
甜味超标 2020-12-05 10:46

I have an MVC4 site using Castle Windsor that I want to add some WebAPI calls to, so I start digging around a little bit on the interwebs.

Now I don\'t know the ins

4条回答
  •  感情败类
    2020-12-05 11:34

    First as Iko stated you need to create a class that implements IHttpControllerActivator.

    Then in ContainerBootstrapper in Bootstrap() add the following Replace the default with the one implemented:

     //This will help in creating Web api with Dependency injection
     GlobalConfiguration.Configuration.Services.Replace(
     typeof(IHttpControllerActivator),
     new WindsorCompositionRoot(container));
    

    Lastly which is not mentioned here and it didn't work for me without it that you should add the following in you implemented IWindsorInstaller service inside install() :

    container.Register(
        Classes.FromThisAssembly()
        .BasedOn()
        .LifestyleTransient()    
    );
    

提交回复
热议问题