1

I'm new to Composite C1 and have the following scenario:

I'm deploying a MVC application as a set of functions to Composite. The thing is that I need to access the session from my MVC functions, but it is null.

I have tested some ideas, but none seems to work for me.

Startup Handler is initialized like this:

public static void OnBeforeInitialize()
{
    var functions = MvcFunctionRegistry.NewFunctionCollection();

    functions.RegisterController<HomeController>("Company.Home");
    functions.RegisterAction<HomeController>("Index", "Company.HomeIndex");

    functions.RouteCollection.MapMvcAttributeRoutes();
    functions.RouteCollection.MapRoute(
        "Default",
        "{controller}/{action}/{id}",
        new { action = "Index", id = UrlParameter.Optional }

        );
}

Then, from any controller a call to session:

protected override void OnActionExecuting(ActionExecutingContext context)
{
    var session = this.Session; // returns null
    var request = this.Request; // this one is ok
    var response = this.Response; // this one is ok too
    // ...
}

returns null.

Any help is really appreciated.

Community
  • 1
  • 1
Andres
  • 1,090
  • 14
  • 30

2 Answers2

0

After almost a day trying to figure out why session is null, I found out that I have access to System.Web.HttpContext.Current.Session from OnActionExecuting. So I'm just using the old HttpSessionStateWrapper trick to make it work:

protected override void OnActionExecuting(ActionExecutingContext context)
{
    var session = System.Web.HttpContext.Current.Session;
    HttpSessionStateBase sessionBase = new HttpSessionStateWrapper(session);
    // ...
}

That still does not solve the question to why Session is null. According to all the references that I read, it should be. Things like controller implementing IRequiresSessionState, routing issues, and many others possible solutions were unsuccessfully tested.

At the end I'm happy because I can reach the session and all its precious data.

Thanks

Andres
  • 1,090
  • 14
  • 30
0

The reason for Session property being null, is that the way MVC function rendering is implemented - a new instance of HttpContext is being created to render the function. This new HttpContext is used for running an MVC pipeline.

You can see the source code here:

https://github.com/Orckestra/C1-Packages/blob/master/Composite.AspNet.MvcFunctions/Composite.AspNet.MvcFunctions/FunctionProvider/MvcFunctionBase.cs#L165

That said, it is a bug, that the reference to current Session isn't passed tot the new HttpContext, and I will look into fixing/updating the package later on.

Dmitry Dzygin
  • 1,258
  • 13
  • 26
  • Done. Uninstalling Composite.AspNet.MvcFunctions and installing a new version (1.0.8) should solve the problem – Dmitry Dzygin Mar 22 '16 at 13:24
  • Hi Dmitry, thanks. However changes didn't seem to work for me. After updating to 1.0.8, removing references in MVC project, adding references back and rebuilding the solution I can see Session is still null. Still all other values are ok. Best. – Andres Mar 23 '16 at 14:43
  • Can you check what is in System.Web.HttpContext.Current.Items["AspSession"] and in HttpContext.Items["AspSession"] when you're inside of the mvc controller code? – Dmitry Dzygin Mar 23 '16 at 15:49
  • Please try one more time, should be fixed in v1.0.9 https://github.com/Orckestra/C1-Packages/issues/21 – Dmitry Dzygin Mar 29 '16 at 08:32
  • I'm sorry I was pulled into another project. I'll do it today. Thanks. – Andres Mar 29 '16 at 14:42