Skip to content

Instantly share code, notes, and snippets.

@korz
Last active December 17, 2015 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save korz/534361a0c9776d971d60 to your computer and use it in GitHub Desktop.
Save korz/534361a0c9776d971d60 to your computer and use it in GitHub Desktop.
Adding Ninject to WebAPI
1. Create a new WebApi project
2. Install the Ninject.MVC3 NuGet package which will in turn
install the Ninject, and Ninject.Web.Common NuGet packages
3. Create a Ninject Folder
4. Create a NinjectResolver.cs class as shown below
using System.Web.Http.Dependencies;
using Ninject;
namespace MyNampespace
{
public class NinjectResolver : NinjectScope, IDependencyResolver
{
private IKernel kernel;
public NinjectResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
public IDependencyScope BeginScope()
{
return new NinjectScope(kernel);
}
}
}
5. Next create a NinjectScope.cs class as shown below
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http.Dependencies;
using Ninject.Activation;
using Ninject.Parameters;
using Ninject.Syntax;
namespace MyNamespace
{
public class NinjectScope : IDependencyScope
{
protected IResolutionRoot resolutionRoot;
public NinjectScope(IResolutionRoot kernel)
{
resolutionRoot = kernel;
}
public object GetService(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null,
new Parameter[0], true, true);
return resolutionRoot.Resolve(request).SingleOrDefault();
}
public IEnumerable<object> GetServices(Type serviceType)
{
IRequest request = resolutionRoot.CreateRequest(serviceType, null,
new Parameter[0], true, true);
return resolutionRoot.Resolve(request).ToList();
}
public void Dispose()
{
}
}
}
6. Create a ICustomerRepository.cs interface
namespace WebApi.Helpers
{
public interface ICustomerRepository
{
IEnumerable<string> GetCustomers();
}
}
7. Create a CustomerRepository.cs class
namespace WebApi.Helpers
{
public class CustomerRepository : ICustomerRepository
{
public IEnumerable<string> GetCustomers()
{
return new List<string> { "CustomerA", "CustomerB"};
}
}
}
8. Create a ServiceModule.cs to hold the bindings as shown below example.
using Ninject.Modules;
namespace MyNamespace
{
public class ServiceModule : NinjectModule
{
public override void Load()
{
Bind<ICustomerRepository>().To<CustomerRepository>().InSingletonScope();
}
}
}
9. If you may have noticed under the App_Start folder Ninject created a class called
NinjectWebCommon.cs. This is where Ninject wires up the Module(s) and directs WebApi
on how to interact with them. There are only a couple of modifications we need to
make to this class, as shown below.
//Add after private static readonly Bootstrapper bootstrapper = new Bootstrapper();
public static IKernel Kernel { get { return bootstrapper.Kernel; } }
//Add to the private static void RegisterServices(IKernel kernel) method
kernel.Load<ServiceModule>();
10. Next, go into the Global.asax.cs file and copy in the following line at the end of
the Application_Start method
GlobalConfiguration.Configuration.DependencyResolver = new NinjectResolver(NinjectWebCommon.Kernel);
11. Now you have Ninject all setup, the following sample controller shows how we use DI.
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebApi.Helpers;
namespace WebApi.Controllers
{
public class CustomerController : ApiController
{
private readonly ICustomerRepository _customerRepository;
public CustomerController(ICustomerRepository customerRepository)
{
_customerRepository = customerRepository;
}
public HttpResponseMessage Get()
{
var customers = _customerRepository.GetCustomers();
return Request.CreateResponse(HttpStatusCode.OK, customers);
}
}
}
@salfab
Copy link

salfab commented Aug 4, 2014

Thank you very much. That gist finally gave me the answer I was looking for!

Any chance this ever gets included out of the box to the Ninject nuget package ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment