How to inject HttpContextBase using Autofac in ASP.NET MVC 4

Brendan Vogt picture Brendan Vogt · Mar 26, 2013 · Viewed 10.8k times · Source

I am using ASP.MVC 4 and Autofac.

I have registered the following in my global.asax.cs file:

ContainerBuilder builder = new ContainerBuilder();

builder.Register(c => c.Resolve<HttpContextBase>().Request)
     .As<HttpRequestBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Response)
     .As<HttpResponseBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Server)
     .As<HttpServerUtilityBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Session)
     .As<HttpSessionStateBase>()
     .InstancePerHttpRequest();

builder.RegisterControllers(Assembly.GetExecutingAssembly());

builder.RegisterType<WebWorkContext>().As<IWorkContext>().InstancePerHttpRequest();

In my Home controller I have this (just for testing purposes):

private readonly HttpContextBase httpContext;

public HomeController(HttpContextBase httpContext)
{
     this.httpContext = httpContext;
}

I used the exact same code with an ASP.NET MVC 3 project and it worked fine. Now in this project I am getting errors. Not sure why? The error that I am getting is:

None of the constructors found with 'Autofac.Core.Activators.Reflection.DefaultConstructorFinder' on type 'MyProject.Web.Controllers.HomeController' can be invoked with the available services and parameters: Cannot resolve parameter 'System.Web.HttpContextBase httpContext' of constructor 'Void .ctor(System.Web.HttpContextBase)'. at Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.Execute() at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1 parameters) at Autofac.Core.Registration.ExternalRegistrySource.<

I'm not too sure why this won't work? Do I need to do things differently in ASP.NET 4?

I have a separate project in which I also want to inject HttpContextBase and I am getting the same error.

Answer

Brendan Vogt picture Brendan Vogt · Mar 26, 2013

Thanks to nemesv.

I ended up replacing:

builder.Register(c => c.Resolve<HttpContextBase>().Request)
     .As<HttpRequestBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Response)
     .As<HttpResponseBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Server)
     .As<HttpServerUtilityBase>()
     .InstancePerHttpRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Session)
     .As<HttpSessionStateBase>()
     .InstancePerHttpRequest();

...with just:

builder.RegisterModule(new AutofacWebTypesModule());

It works now. Not sure what the difference is but the code in the module looks exactly the same as mine above.