Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ninject working with WCF Web API Preview 5

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 another internal WCF Service using the Ninject.Extensions.Wcf library. However I cannot get it to work when creating a new MVC 3 project and getting the WebApi.All library from NuGet.

I have looked at this stackoverflow post Setting up Ninject with the new WCF Web API but cannot get it working which I believe could be to do with some of the changes in the latest release.

I am also unsure which Ninject Libraries to reference beyond the main one. Do I use the Ninject.MVC3 , Ninject.Extensions.Wcf.

Any help on this would be much appreciated.

****UPDATE**

Code I am using which is from the answer in the question mentioned above. I have this in its own class file.

   public class NinjectResourceFactory : IResourceFactory
    {
        private readonly IKernel _kernel;

        public NinjectResourceFactory(IKernel kernel)
        {
            _kernel = kernel;
        }

        public object GetInstance(Type serviceType, InstanceContext instanceContext, HttpRequestMessage request)
        {
            return _kernel.Get(serviceType);
        }

        public void ReleaseInstance(InstanceContext instanceContext, object service)
        {
            // no op
        }
    }

This I have in my global.asax:

var configuration = HttpConfiguration.Create().SetResourceFactory(new NinjectResourceFactory());
RouteTable.Routes.MapServiceRoute<myResource>("resource", configuration);

The issue I am having is that the IResourceFactory interface is not recognised and that the HttpConfiguration.Create() no longer exists so I need to set the SetResourceFactory some other way which I have tried to do using the HttpConfiguration().CreateInstance method but no joy.

like image 211
Cragly Avatar asked Sep 29 '11 19:09

Cragly


3 Answers

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<IProductRepository>().To<MockProductRepository>();
    }

}

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<ContactsApi>("api/contacts", config);
}
like image 150
zanewill Avatar answered Dec 09 '22 15:12

zanewill


In P5 you have to derive from WebApiConfiguration and use your derived configuration:

public class NinjectConfiguration : WebApiConfiguration
    {
        public NinjectConfiguration(IKernel kernel)
        {
            CreateInstance((t, i, m) =>
            {
                return kernel.Get(t);
            }); 
        }
    }
like image 25
Alexander Zeitler Avatar answered Dec 09 '22 17:12

Alexander Zeitler


There are great answers to the question here but I would like to show you the way with default WebApi configuration:

    protected void Application_Start(object sender, EventArgs e) {

        RouteTable.Routes.SetDefaultHttpConfiguration(new Microsoft.ApplicationServer.Http.WebApiConfiguration() { 
            CreateInstance = (serviceType, context, request) => GetKernel().Get(serviceType)
        });

        RouteTable.Routes.MapServiceRoute<People.PeopleApi>("Api/People");
    }

    private IKernel GetKernel() { 

        IKernel kernel = new StandardKernel();

        kernel.Bind<People.Infrastructure.IPeopleRepository>().
            To<People.Models.PeopleRepository>();

        return kernel;
    }

The below blog post talks a little bit about Ninject integration on WCF Web API:

http://www.tugberkugurlu.com/archive/introduction-to-wcf-web-api-new-rest-face-ofnet

like image 34
tugberk Avatar answered Dec 09 '22 16:12

tugberk