21

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.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • 3
    I don't see in your code where do you register the `HttpContextBase` itself... Try to register the `AutofacWebTypesModule` with `builder.RegisterModule(new AutofacWebTypesModule());` – nemesv Mar 26 '13 at 13:32
  • Yes I just saw this now on their website. Do I register `AutofacWebTypesModule` first before I do `builder.Register(c => c.Resolve().Request)`..? – Brendan Vogt Mar 26 '13 at 13:36
  • I this case the registration order should not matter. – nemesv Mar 26 '13 at 13:37

1 Answers1

34

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.

Brendan Vogt
  • 25,678
  • 37
  • 146
  • 234
  • 19
    If you check the source code of the [AutofacWebTypesModule](https://code.google.com/p/autofac/source/browse/src/Source/Autofac.Integration.Web.Mvc3/AutofacWebTypesModule.cs?r=94f70ab10f4d65991c600e2e80171ce4847589e6) you can see that you have left out the most important line `builder.Register(c => new HttpContextWrapper(HttpContext.Current)) .As() .InstancePerHttpRequest();` which registers the `HttpContextBase` – nemesv Mar 26 '13 at 14:51