如何在ASP.NET MVC 4中使用Autofac注入HttpContextBase

Bre*_*ogt 21 c# asp.net asp.net-mvc autofac asp.net-mvc-4

我正在使用ASP.MVC 4Autofac.

我在我的global.asax.cs文件中注册了以下内容:

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();
Run Code Online (Sandbox Code Playgroud)

在我的家庭控制器中我有这个(仅用于测试目的):

private readonly HttpContextBase httpContext;

public HomeController(HttpContextBase httpContext)
{
     this.httpContext = httpContext;
}
Run Code Online (Sandbox Code Playgroud)

我使用完全相同的代码与ASP.NET MVC 3项目,它工作正常.现在在这个项目中我遇到了错误.不知道为什么?我得到的错误是:

与类型"Autofac.Core.Activators.Reflection.DefaultConstructorFinder"发现施工人员无"MyProject.Web.Controllers.HomeController"可以与提供的服务和参数来调用:无法解析参数"System.Web.HttpContextBase的HttpContext"的构造函数'Void .ctor(System.Web.HttpContextBase)'.at autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(IComponentContext context,IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable1参数)at Autofac.Core.Resolving.InstanceLookup.Execute()at Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope,IComponentRegistration registration,IEnumerable)1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable1个参数)在Autofac.Core.Registration.ExternalRegistrySource.<

我不太清楚为什么这不起作用?我需要在ASP.NET 4中以不同的方式做事吗?

我有一个单独的项目,我也想注入HttpContextBase,我得到同样的错误.

Bre*_*ogt 34

谢谢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();
Run Code Online (Sandbox Code Playgroud)

......只是:

builder.RegisterModule(new AutofacWebTypesModule());
Run Code Online (Sandbox Code Playgroud)

它现在有效.不确定区别是什么,但模块中的代码与上面的代码完全相同.

  • 如果您查看[AutofacWebTypesModule]的源代码(https://code.google.com/p/autofac/source/browse/src/Source/Autofac.Integration.Web.Mvc3/AutofacWebTypesModule.cs?r=94f70ab10f4d65991c600e2e80171ce4847589e6)你可以看到你遗漏了最重要的一行`builder.Register(c => new HttpContextWrapper(HttpContext.Current)).作为<HttpContextBase>().InstancePerHttpRequest();`注册`HttpContextBase` (18认同)