私はhttpリクエストをトレースするカスタムHttpModuleを持っていますが、実装の一部は以下の通りです。HttpModuleへのコンストラクタインジェクションを使用してHttpContextを注入していますか?
private readonly HttpContextBase _httpContext;
private readonly ISessionContext _sessionContext;
public ASHttpModule(HttpContextBase httpContext,
ISessionContext sessionContext)
{
this._httpContext = httpContext;
this._sessionContext = sessionContext;
}
public void Init(HttpApplication context)
{
context.BeginRequest += Context_BeginRequest;
context.EndRequest += Context_EndRequest;
}
private void Context_BeginRequest(object sender, EventArgs e)
{
Stopwatch stopwatch = new Stopwatch();
_httpContext.Items["Stopwatch"] = stopwatch;
stopwatch.Start();
}
private void Context_EndRequest(object sender, EventArgs e)
{
Stopwatch stopwatch = (Stopwatch)_httpContext.Items["Stopwatch"];
if (stopwatch == null)
return;
stopwatch.Stop();
TimeSpan ts = stopwatch.Elapsed;
//Check current httprequest variables and log if have to
}
ここで私の依存関係の登録(Autofacを使用)。
builder.RegisterType<WebSessionContext>()
.As<ISessionContext>().InstancePerRequest();
builder.Register(c => (new HttpContextWrapper(HttpContext.Current) as HttpContextBase))
.As<HttpContextBase>()
.InstancePerRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Request)
.As<HttpRequestBase>()
.InstancePerRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Server)
.As<HttpServerUtilityBase>()
.InstancePerRequest();
builder.Register(c => c.Resolve<HttpContextBase>().Session)
.As<HttpSessionStateBase>()
.InstancePerRequest();
問題は、ここでのHttpContextが要求ごとに注入する必要がありながら、HttpModuleを一度だけ構成されています。私が見つけたソリューションは、DependencyResolverを次のように使用しています。
HttpContextBase _httpContext = DependencyResolver.Current.GetService<HttpContextBase>();
ただし、ServiceLocatorはアンチパターンと見なされますので、この使用を避けたいと思います。
DependencyResolverを使用せずにHttpContextをHttpModuleに挿入する方法はありますか?
→問題の原因をあざけるあなたは[あなたのコンポーネントにランタイムデータ]を注入していることである/テスト(https://www.cuttingedge.it/blogs/steven/pivot/entry.php?id=99)。 – Steven
ブログの投稿をありがとう。私の間違いを見るのを助けました。エレガントなソリューション。 –