Over a million developers have joined DZone.

Using Dependency Injection with ASP.NET Web API Hosted on Katana

DZone's Guide to

Using Dependency Injection with ASP.NET Web API Hosted on Katana

Free Resource

Using Katana we can build a light-weight server to host an ASP.NET application, without needing IIS or System.Web. Katana has a nice support for hosting SignalR and Web API with very few lines of code. In a previous post, we saw how to self-host SignalR using Katana. In this post, we will see how easy it is to perform dependency injection on a self-hosted Katana application. 

Open Visual Studio 2013 and create a new console application. Add following NuGet packages to the application: 

  • Install-Package Microsoft.AspNet.WebApi.Owin
  • Install-Package Ninject

First package gets a bunch of packages for creating and hosting Web API. The second package installs theNinject IoC container

Add an API Controller to the application and change the code of the controller to: 

public class ProductsController : ApiController
    IProductsRepository repository;

    public ProductsController(IProductsRepository _repository)
        repository = _repository;

    public IEnumerable<Product> Get() 
        return repository.GetAllProducts();

We need to inject instance of a concrete implementation of IProductRepository. We will do this using dependency injection. You can create the repository interface and an implementation class by your own. 

To perform dependency injection with Web API, the WebApiContrib project has a set of NuGet packages, including one for Ninject. But the version of Web API it requires is 4.0.30319. As we are using VS 2013, the default version of Web API is Installing this NuGet package may result into version compatibility issues. To avoid this issue, we can build the WebapiContrib.Ioc.Ninject project using VS 2013 or we can also copy the code of NinjectResolver class from the GitHub repo and add it to the console application. 

In the Owin Startup class, we need to configure Web API and also tie it with an instance of NinjectResolver. Constructor of NinjectResolver accepts an argument of IKernel type. Kernel is the object where we define the mapping between requested types and types to be returned. Let’s create a class with a static method; this method will create the kernel for us. Add a class and name it NinjectConfig. Change code of the class as: 

public static class NinjectConfig
    public static IKernel CreateKernel()
        var kernel = new StandardKernel();

            return kernel;
        catch (Exception)

Now all we need to do is, configure Web API and start the server. Add a class, change the name as Startup and replace the code as: 

public class Startup
    public void Configuration(IAppBuilder app)
        var config = new HttpConfiguration();
        config.DependencyResolver = new NinjectResolver(NinjectConfig.CreateKernel());

        config.Routes.MapHttpRoute("default", "api/{controller}/{id}", new { id=RouteParameter.Optional });


Finally, start the server in Main method: 

static void Main(string[] args)
    string uri = "http://localhost:8080/";

    using (WebApp.Start<Startup>(uri))
        Console.WriteLine("Server started...");
        Console.WriteLine("Server stopped!");

Open a browser and enter the following URL: 

You should be able to see list of products returned from the repository class. 

Happy coding!


Published at DZone with permission of Rabi (ravi) Kiran, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}