2

I have mvc4 WebAPI application with castle Windsor interception.
The interceptor class needs HttpContext.Current.Session.
When I call it directly from the interceptor, it is null.
So I read here that I need to inject the Session and not just access it in the interceptor.

This the code I ended up with...

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());
  }
}

Is there any other way to access the session from the interceptor?

Thanks

SexyMF
  • 10,657
  • 33
  • 102
  • 206

1 Answers1

3

I always forget that WEBAPI is not MVC.
this is not castle issue.

this did the trick!

public override void Init()
{
    this.PostAuthenticateRequest += MvcApplication_PostAuthenticateRequest;
    base.Init();
}

void MvcApplication_PostAuthenticateRequest(object sender, EventArgs e)
{
    System.Web.HttpContext.Current.SetSessionStateBehavior(
        SessionStateBehavior.Required);
}

https://stackoverflow.com/a/15038669/936651
+1 to @Soren

Community
  • 1
  • 1
SexyMF
  • 10,657
  • 33
  • 102
  • 206