Popular Posts
Build an OpenVPN server on android device Preparation An android device, in this case, Sony xperia Z is used Root permission required Linux Deploy for deploy i... javax.net.ssl.SSLHandshakeException: Connection closed by peer in Android 5.0 Lollipop Recently, there is a error occurs when access website via ssl connection like below although it worked fine several days ago. // Enable SSL... netbean shortcut Ctrl + F:尋找 F3:尋找下一個字串 Ctrl + G:跳到第 N 行 Ctrl + H:取代 Tab:增加縮排 Shift + Tab:減少縮排 Ctrl + E:刪除一行 Ctrl + Shift + I:修正 import 項目 Alt + Ent...
Stats
Session State Locks cause pending requests

In some scenario, while there is a long pending request exist, it may cause other request pending because session data is locked by the first request. Here is a example of session lock.

First I have a controller with default session policy

using System;
using System.Collections.Generic;
using System.Drawing.Imaging;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using WebApplication2.Models;

namespace WebApplication2.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LongTimeout()
        {
            System.Threading.Thread.Sleep(30000);
            return Json(new { code = 200 }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ShortTimeout()
        {
            return Json(new { code = 200 }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult Captcha()
        {
            var captcha = MathCaptcha.Generate();
            Session["captcha"] = captcha.Answer;

            return File(captcha.GetImageData(ImageFormat.Png), "image/png");
        }
    }
}

And then create another controller that make session state readonly

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace WebApplication2.Controllers
{
    [SessionState(System.Web.SessionState.SessionStateBehavior.ReadOnly)]
    public class SessionLessController : Controller
    {
        public ActionResult LongTimeout()
        {
            System.Threading.Thread.Sleep(30000);
            return Json(new {code=200 }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult ShortTimeout()
        {
            return Json(new { code = 200 }, JsonRequestBehavior.AllowGet);
        }
    }
}

Request is pending because the session of first request is locked

After first request completed, other requests will go on

Request won't be block when session state is set as readonly

[Json.net] Convert enum to string
[JsonConverter(typeof(StringEnumConverter))]
public enum Gender
{
    [EnumMember(Value = "man")]
    Male,
    [EnumMember(Value = "woman")]
    Femail,
    [EnumMember(Value = "unknown")]
    Unknown
}