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(IEnumerable
1 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, IEnumerable
1 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.
The HttpContextBase class is an abstract class that contains the same members as the HttpContext class. The HttpContextBase class enables you to create derived classes that are like the HttpContext class, but that you can customize and that work outside the ASP.NET pipeline.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With