HttpHandler with Session State


If you are developing httphandler in asp.net realized that httphandlers can not use session as default.If you want to access session object do it as below.

Add  “System.Web.SessionState” namespace to your handler code.

There are two interface . one of them  must be inherited by your httphandler object:

IRequiresSessionState Interface

Specifies that the target HTTP handler requires read and write access to session-state values. This is a marker interface and has no methods.

IReadOnlySessionState Interface
Specifies that the target HTTP handler requires only read access to session-state values. This is a marker interface and has no methods.
 
 Example :
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;
 
namespace PostmanExamples
{
 
    // IRequireSessionState if you want write access
    public class MyHttpHandlerWithSessionState
        : IHttpHandler, IReadOnlySessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            string s = context.Session["SomeSessionVariable"];
        }
 
        public bools IsReusable { get { return true; } }
    }
}
Advertisements

About bpostaci
Escalation Engineer in Microsoft.

2 Responses to HttpHandler with Session State

  1. muakacho says:

    Excellent article.
    I need help. MyHandler is work very well:


    public class MyHandler : IHttpHandler, IRequiresSessionState
    {

    public bool IsReusable
    {
    get { return true; }
    }

    public void ProcessRequest(HttpContext context)
    {
    try
    {
    List appsIDs;

    if (context.Session != null)
    {
    if (context.Session["apps"] != null)
    {
    appsIDs = (List)context.Session["apps"];
    }
    else
    appsIDs = new List();

    int appID = 0;
    if (!String.IsNullOrEmpty(context.Request.Params["appID"]))
    {
    appID = Convert.ToInt16(context.Request.Params["appID"]);
    appsIDs.Add(appID);
    context.Session.Add("apps", appsIDs);
    context.Response.Write("AppID added:" + appID);
    }
    }

    }
    catch (Exception ee)
    {
    twrow ee;
    }

    }
    }

    http://mysite/_layouts/myhandler.ashmx?appID=232 is added to sessions

    But, for example, some page of same web application (http://mysite/SitePages/some.aspx) don’t access to this values:


    protected override void OnInit(EventArgs e)
    {
    StringBuilder s = new StringBuilder();
    try
    {
    if (Session != null)
    {
    s.Append("not null");
    if (Session["apps"] != null)
    {
    List appsIDs = (List)Session["apps"];
    foreach (string appID in appsIDs)
    {
    s.Append(appID);
    }
    }

    }
    else
    s.Append("null");
    }
    catch
    {
    s.Append("Exception");
    }
    Message = s.ToString();
    base.OnInit(e);
    }

    Any ideas?

    • bpostaci says:

      Try to use httpModule instead of httpHandler. (I guess you try to use a variable like appIDs in application context). If you give me more detail about your bussiness logic i will help you.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: