这是运行.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请求依赖,永远不会从容器本身请求.
所以我发现了这个:@L_502_1@
而我这样做了:
- 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 Reference Exception.
奇怪的是,context.Response不是null,当我调用它时抛出的.Redirect().
我想知道是否使用.InstancePerLifetimeScope();是问题.
顺便说一句,我尝试使用Response.Redirect(),它完美无缺.
那可能是什么问题呢?
谢谢,
驰