ASP.NET Web API 2 with Autofac Owin
2016/2/292 min read
bookmark this
This blog shows how to setup Autofac at ASP.NET Web API 2 with Owin.
packages.config
Following is the package.config, you can use this nuget command to install all
the these to your project.Update-Package -ProjectName MyProject -reinstall
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Autofac" version="3.5.0" targetFramework="net452" />
<package id="Autofac.Owin" version="3.1.0" targetFramework="net452" />
<package id="Autofac.WebApi" version="3.1.0" targetFramework="net452" />
<package id="Autofac.WebApi2" version="3.4.0" targetFramework="net452" />
<package id="Autofac.WebApi2.Owin" version="3.3.0" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Core" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.AspNet.WebApi.Owin" version="5.2.3" targetFramework="net452" />
<package id="Microsoft.Net.Compilers" version="1.0.0" targetFramework="net452" developmentDependency="true" />
<package id="Microsoft.Owin" version="3.0.1" targetFramework="net452" />
<package id="Microsoft.Owin.Host.SystemWeb" version="3.0.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net452" />
<package id="Owin" version="1.0" targetFramework="net452" />
</packages>
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<IList>(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 implement 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(); } } }