Just like Mark said, you need to register these http-dependent services either as PerWebRequest or Transient. Here's a sample that shows how to register and inject a HttpRequest or HttpContext:
public class Service {
private readonly HttpRequestBase request;
public Service(HttpRequestBase request) {
this.request = request;
}
public string RawUrl {
get {
return request.RawUrl;
}
}
}
...
protected void Application_Start(object sender, EventArgs e) {
IWindsorContainer container = new WindsorContainer();
container.AddFacility<FactorySupportFacility>();
container.AddComponentLifeStyle<Service>(LifestyleType.Transient);
container.Register(Component.For<HttpRequestBase>()
.LifeStyle.PerWebRequest
.UsingFactoryMethod(() => new HttpRequestWrapper(HttpContext.Current.Request)));
container.Register(Component.For<HttpContextBase>()
.LifeStyle.PerWebRequest
.UsingFactoryMethod(() => new HttpContextWrapper(HttpContext.Current)));
}
By using HttpRequestBase
instead of HttpRequest
you can easily mock it out for testing.
Also, don't forget to register PerWebRequestLifestyleModule
in your web.config.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…