Read or Write Session value on HTTP Handler file (.ashx)

Posted by Venkat | Labels: ,

How to use Session (ie: read or Write) on HTTPHandler file ie: .ashx file ?

I noticed some post on forums how to get the Session value on (.ashx file). So here is the solution.
By Default if you use Session on handler, the session returns null value. You have to implement the interface to read or write session value on handler file. If there is a situation to read Session value on your Handler file  implement IReadOnlySessionState.

When there is a case, you have to read or write session value in Handler file.
Implement IRequireSessionState.

Here is the code where i implement both IReadOnlySessionState and IRequireSessionState.

Example:

<%@ WebHandler Language="C#" Class="Handler" %>

using System;
using System.Web;
using System.Web.SessionState; 

public class Handler : IHttpHandler , IReadOnlySessionState  , IRequiresSessionState   {
    
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        context.Response.Write("Hello World");
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}

PayOffers.in