Ninject working with WCF Web API Preview 5

后端 未结 3 1770
我寻月下人不归
我寻月下人不归 2021-01-04 23:18

Can anybody point me in the right direction to get Ninject working with WCF Web API Preview 5? I have it successfully up and running in my ASP.NET MVC 3 project and also in

3条回答
  •  执念已碎
    2021-01-04 23:45

    Following is my code with Ninject and WebApi,it works. Create a class inherites from WebApiConfiguration

    public class NinjectWebApiConfiguration : WebApiConfiguration {
        private IKernel kernel = new StandardKernel();
    
        public NinjectWebApiConfiguration() {
            AddBindings();
            CreateInstance = (serviceType, context, request) => kernel.Get(serviceType);
        }
    
        private void AddBindings() {
            kernel.Bind().To();
        }
    
    }
    

    and use the NinjectWebApiConfiguration in RegisterRoutes

    public static void RegisterRoutes(RouteCollection routes) {
    
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
        var config = new NinjectWebApiConfiguration() { 
            EnableTestClient = true
        };
    
        routes.MapServiceRoute("api/contacts", config);
    }
    

提交回复
热议问题