これは、.Net 3.5 を実行する Asp.net アプリケーション (MVC ではない) です。
これは私がしました:
protected void Application_Start(object sender, EventArgs e)
{
...
builder.Register(c => new HttpContextWrapper(HttpContext.Current))
.As<HttpContextBase>()
.InstancePerHttpRequest();
}
しかし、うまくいきません。
私がこれを得ているエラー:
「httpRequest」に一致するタグを持つスコープは、インスタンスが要求されたスコープから見えません。これは通常、HTTP ごとのリクエストとして登録されたコンポーネントが、SingleInstance() コンポーネント (または同様のシナリオ) によって要求されていることを示します。Web 統合では、コンテナー自体からではなく、常に DependencyResolver.Current または ILifetimeScopeProvider.RequestLifetime から依存関係を要求します。 .
それで、私はこれを見つけました: https://stackoverflow.com/a/7821781/305469
そして、私は代わりにこれをしました:
builder.Register(c => new HttpContextWrapper(HttpContext.Current))
.As<HttpContextBase>()
.InstancePerLifetimeScope();
しかし、今私がこれを行うとき:
public class HttpService : IHttpService
{
private readonly HttpContextBase context;
public HttpService(HttpContextBase context)
{
this.context = context;
}
public void ResponseRedirect(string url)
{
//Throws null ref exception
context.Response.Redirect(url);
}
}
Null参照例外が発生しました。
奇妙なことに、context.Response は null ではありません。スローされるのは .Redirect() を呼び出すときです。
.InstancePerLifetimeScope(); を使用しているかどうか疑問に思っています。が問題です。
ところで、私は Response.Redirect() を使用してみましたが、完全に機能します。
では、何が問題になるのでしょうか?
ありがとう、
カイ