Я использую ASP.MVC 4
и Autofac
.
Я зарегистрировал следующее в моем файле 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();
В моем домашнем контроллере у меня есть это (только для целей тестирования):
private readonly HttpContextBase httpContext;
public HomeController(HttpContextBase httpContext)
{
this.httpContext = httpContext;
}
Я использовал тот же самый код с проектом ASP.NET MVC 3, и он работал нормально. Теперь в этом проекте возникают ошибки. Не знаете почему? Ошибка, которую я получаю:
Ни один из конструкторов, найденных с помощью "Autofac.Core.Activators.Reflection.DefaultConstructorFinder" в типе "MyProject.Web.Controllers.HomeController", может быть вызван доступными службами и параметрами: невозможно разрешить параметр "Система". Web.HttpContextBase httpContext 'конструктора' Void.ctor(System.Web.HttpContextBase) '. в Autofac.Core.Activators.Reflection.ReflectionActivator.ActivateInstance(контекст IComponentContext, IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.Activate(IEnumerable
1 параметр) в Autofac.Core.Resolving.InstanceLookup.Execute() в Autofac.Core.Resolving.ResolveOperation.GetOrCreateInstance(ISharingLifetimeScope currentOperationScope, IComponentRegistration регистрация, IEnumerable 1 parameters) at Autofac.Core.Resolving.InstanceLookup.ResolveComponent(IComponentRegistration registration, IEnumerable
1 параметр) в Autofac.Core.Registration.ExternalRegistrySource. <
Я не слишком уверен, почему это не сработает? Мне нужно делать что-то по-другому в ASP.NET 4?
У меня есть отдельный проект, в котором я также хочу ввести HttpContextBase
, и я получаю ту же ошибку.