Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session in generic handler?

I have the following simple handler (removed some code for vissibilty sakes, but the below still fails)

<%@ WebHandler Language="C#" Class="DownloadHandler" %>
using System;
using System.Web;

public class DownloadHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        if (context.Session["t1"] != "true")
        {

        }
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}

The line if (context.Session["t1"] != "true") is failing with "Object reference not set to an instance of an object." and i dont quite get why that is?

like image 460
brother Avatar asked Mar 10 '12 16:03

brother


1 Answers

That's because for http handler in order to access Session you need to explicitly implement IRequiresSessionState interface.

Keep in mind that if you do that there will be an implicit locking on the session object and you won't be able to have multiple handlers in the same session state processed simultaneously.

There is an IReadOnlySessionState interface as well for read-only session state access.

like image 87
Vladimir Makaev Avatar answered Oct 20 '22 09:10

Vladimir Makaev