如何在ASP.NET中使用Autofac MVC 4注入HttpContextBase [英] How to inject HttpContextBase using Autofac in ASP.NET MVC 4
问题描述
我使用 ASP.MVC 4
和 Autofac
。
我已经注册在我的的global.asax.cs
文件以下内容:
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;
}
我用完全相同的code与ASP.NET MVC 3项目,它工作得很好。现在,在这个项目中我得到的错误。不知道为什么?那我得到的错误是:
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:
与类型'MyProject.Web.Controllers.HomeController'Autofac.Core.Activators.Reflection.DefaultConstructorFinder'发现的建设者都不能在可用的服务和参数调用:无法解析参数'系统。 Web.HttpContextBase HttpContext的虚空.ctor(System.Web.HttpContextBase)''构造。在Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance在Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable的 1参数)在Autofac(IComponentContext背景下,IEnumerable的 1参数) .Core.Resolving.InstanceLookup.Execute()在Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration登记的IEnumerable
1参数)在Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration注册,IEnumerable的
1参数)在Autofac.Core.Registration.ExternalRegistrySource&LT;
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.<
我也不太清楚为什么这是不行的?我是否需要在ASP.NET 4做不同的事情呢?
I'm not too sure why this won't work? Do I need to do things differently in ASP.NET 4?
我有一个单独的项目中,我也想注入 HttpContextBase
,我得到同样的错误。
I have a separate project in which I also want to inject HttpContextBase
and I am getting the same error.
推荐答案
感谢 nemesv
。
我最终取代:
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.RegisterModule(new AutofacWebTypesModule());
现在的工作。不知道什么区别不过是模块中的code长得一模一样的矿井上方。
It works now. Not sure what the difference is but the code in the module looks exactly the same as mine above.
这篇关于如何在ASP.NET中使用Autofac MVC 4注入HttpContextBase的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!