.NET · IoC · Visual Studio Code

IoC in ASP.NET Web API projects

You probably want structure your project so that you can separate concerns. In our traditional applications, we are calling into reusable libraries to handle tasks, but with inversion of control (IoC) it is the framework that is calling into the code. By using IoC we are increasing modularity and are making our service more extensible.

There are a lot of containers for Inversion of Control out there, I am using Autofac in this sample. I am not going into Interceptors in this post, but if you want logging in you service, I highly recommend digging into that.

You can download the project here.

1. Create a new ASP.NET Web Application with an Empty template and set true on the checkbox for Web Api

2. Add 2 new Project to your solution as a C# class library. The first will be the business layer and the second will be our DTO’s

My projects in this demo are:

• IOCAutofac.API (ASP.NET Web API Project)
• IOCAutofac.Services (The business layer, C# class library)
• IOCAutofac.Models (The DTO’s, C# class library)

On the API project make sure to get these 2 packages with NuGet Package manager

• Autofac
• Autofac.WebApi2


3. I will create a function in the business layer just to set things up for our API project.

namespace IOCAutofac.Services
    public class TestService
        public async Task<string> GetPing()
            return "Pong";

4. Add reference in you API project to the Service and Models project.


5. Add a new class to your App_Start folder in you API project named IocConfig.cs. keep the namespace without the .App_Start.

Add the following referances:

using System.Web.Http;
using System.Reflection;
using Autofac;
using Autofac.Integration.WebApi;
using IOCAutofac.Services;

namespace IOCAutofac.API
    public class IocConfig
        public static void Configure()
            var builder = new ContainerBuilder();
            var container = builder.Build();
            var resolver = new AutofacWebApiDependencyResolver(container);
            GlobalConfiguration.Configuration.DependencyResolver = resolver;

6. In your Global.asax.cs file in Application_Start() function add IocConfig.Configure();

public class WebApiApplication : System.Web.HttpApplication
        protected void Application_Start()

7. Create a new Controller: TestController. Since we now have resolved our TestService with the Autofac container we can inject that service to our constructor in our controller.

public class TestController : ApiController
        private TestService _testService;
        public TestController(TestService testService)
            _testService = testService;

8. Create a contract endpoint and call your business layer.

public async Task<HttpResponseMessage> GetPing()
    var ping = await _testService.GetPing();
        return Request.CreateResponse(HttpStatusCode.OK, ping);

9. Put a breakpoint on constructor in the TestController and you will see that the TestService is resolved.


That’s it.

Thanks for reading.