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

11,250

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.

Share:
11,250
Brendan Vogt
Author by

Brendan Vogt

Wedding photographer and videographer from Paarl, South Africa. Join me on my new adventure in wedding photography and videography at Brendan Vogt Photo &amp; Video.

Updated on June 06, 2022

Comments

  • Brendan Vogt
    Brendan Vogt almost 2 years

    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.

  • nemesv
    nemesv about 11 years
    If you check the source code of the AutofacWebTypesModule you can see that you have left out the most important line builder.Register(c => new HttpContextWrapper(HttpContext.Current)) .As<HttpContextBase>() .InstancePerHttpRequest(); which registers the HttpContextBase