1
我有mvc4 WebAPI应用程序与城堡Windsor拦截。
拦截器类需要HttpContext.Current.Session
。
当我从拦截器直接调用它时,它是空的。
所以我在这里读到我需要注入会话,而不是在拦截器中访问它。HttpContext.Current.Session在将其注入拦截器/或在内部拦截器中使用时为空(mvc4 webapi)
我结束了代码...
protected void Application_Start()
{
ControllerBuilder.Current.SetControllerFactory(new WindsorControllerFactory(container.Kernel));
this.RegisterDependencyResolver();
this.container.Install(new WindsorWebApiInstaller());
}
public class WindsorWebApiInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
// the following interceptor needs Session object
container.Register(Component.For<IInterceptor>()
.ImplementedBy<SecurityInterceptor>()
.LifestylePerWebRequest()
.Named("SecurityInterceptor"));
// session is null here, So Castle wont inject it and throw exception...
container.Register(
Component.For<HttpSessionStateBase>().UsingFactoryMethod(
() => new HttpSessionStateWrapper(HttpContext.Current.Session)).LifestylePerWebRequest());
}
}
是否有任何其他方式自拦截访问会话?
谢谢