Readonly session state in ASP.NET MVC for MONO

753 Views Asked by At

I've been using MVC 5 with master branch of mono and I suspect mono that there's missing implementation for following attribute:

[SessionState(SessionStateBehavior.ReadOnly)]

I tried to decorate selected two controllers with the attribute and Thread.Sleep (5000). As results present these two requests were executed sequentially, not as one expect in parallel.

To give a complete information, I've been using mod_mono (also master branch).

Do you have experiences with parallel execution for a single session?

Thanks!

2

There are 2 best solutions below

1
On

Please check references to: https://github.com/mono/mono/blob/effa4c07ba850bedbe1ff54b2a5df281c058ebcb/mcs/class/System.Web/System.Web.SessionState_2.0/SessionStateBehavior.cs

Please note that some web servers, like apache, may block a concurrent request from the same user for resources other than images under certain circunstances. If that is your case, let me know to provide you more details.

0
On

I had the same problem, with fastcgi-mono-server4 + nginx and with xsp4 stand-alone, so I concluded that it must be a bug (or some non-implemented feature) in this version of mono (debian distribution 3.8-2).

I worked around the problem setting <sessionState mode="Off" /> in web.config and handling the session on the server side with a System.Runtime.Caching.MemoryCache with keys composed from a GUID (for session ID emulation) and the actual string.

To set the cookie I wrote this override in my controller base class

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
    if(HttpContext.Request.Cookies["sid"] == null
        && HttpContext.Response.Cookies["sid"] == null) {
        HttpCookie htsid = new HttpCookie("sid", Guid.NewGuid().ToString());
        HttpContext.Response.Cookies.Set(htsid);
    }
    //...
}

and I added a little helper to obtain the session ID in the same base class

internal string sid {
    get {
        HttpCookie htk = HttpContext.Request.Cookies["sid"];
        if(htk == null) {
            htk = HttpContext.Response.Cookies["sid"];
        }

        return htk == null ? null : htk.Value;
    }
}

This solved my problem on Mono, where I do need concurrent connections from the same session and I need to keep state values per-session, but your mileage may vary.