Session in a generic handler?

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

<%@ 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 string if (context.Session["t1"] != "true")fails: "The reference to the object is not set to the instance of the object." and I don’t understand why this is?

+5
source share
1 answer

For access to the SessionHTTP handler, you must explicitly implement the interface IRequiresSessionState.

Keep in mind that if you do this, there will be an implicit lock on the session object, and you will not be able to process multiple handlers in the same session state at the same time.

There is also an interface IReadOnlySessionStatefor accessing a read-only session.

+11
source

All Articles