ASP.NET Web API 2 with Autofac Owin
2016/02/292 min read
bookmark this
Table of Contents
- Introduction
- packages.config
- Startup Class
- API Controller Class: TestController
- Interface
- Class Implementing Interface
- Autofac Service Module
- Conclusion
Introduction
This blog shows how to set up Autofac for dependency injection in ASP.NET Web API 2 with Owin.
packages.config
Following is the packages.config. You can use this NuGet command to install all
of these to your project: Update-Package -ProjectName MyProject -reinstall
<?xml version="1.0" encoding="utf-8"?>
Startup Class
using Autofac;
using Autofac.Integration.WebApi;
using Microsoft.Owin;
using Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web;
using System.Web.Compilation;
using System.Web.Http;
using WebApplication2.Controllers;
[assembly: OwinStartup(typeof(WebApplication2.Startup))]
namespace WebApplication2
{
public class Startup
{
public void Configuration(IAppBuilder app)
{
HttpConfiguration config = new HttpConfiguration();
// register api routing.
WebApiConfig.Register(config);
app.UseWebApi(config);
var builder = new ContainerBuilder();
var assemblies = BuildManager.GetReferencedAssemblies().Cast().ToArray();
builder.RegisterAssemblyModules(assemblies);
builder.RegisterApiControllers(assemblies);
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
}
}
API Controller Class: TestController
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace WebApplication2.Controllers
{
public class TestController : ApiController
{
Interface1 _i;
public TestController(Interface1 i)
{
_i = i;
}
public IHttpActionResult Get()
{
return Ok(new List() { "Hi", "I", "'m", "Just", "testing" });
}
}
}
Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebApplication2
{
public interface Interface1
{
string Do();
}
}
Class Implementing Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2
{
public class Class1 : Interface1
{
public string Do()
{
return Guid.NewGuid().ToString();
}
}
}
Autofac Service Module
using Autofac;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace WebApplication2
{
public class ServiceModule : Module
{
protected override void Load(ContainerBuilder builder)
{
builder.RegisterType().As().InstancePerDependency();
}
}
}
Conclusion
With Autofac and Owin, you can easily set up dependency injection in your ASP.NET Web API 2 project. The key steps are configuring the Owin startup class, registering your controllers and services with Autofac, and creating service modules for your dependency bindings.