私はとを使用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プロジェクトでまったく同じコードを使用しましたが、正常に機能しました。このプロジェクトでは、エラーが発生しています。理由がわかりませんか?私が得ているエラーは次のとおりです。
タイプ「MyProject.Web.Controllers.HomeController」の「Autofac.Core.Activators.Reflection.DefaultConstructorFinder」で見つかったコンストラクターは、使用可能なサービスとパラメーターで呼び出すことができません。パラメーター「System.Web.HttpContextBasehttpContext」を解決できません。コンストラクター'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
したいのですが、同じエラーが発生します。