3

I have used the below code in app code session.cs file. It has been loaded initially in httpmodule.cs, whenever every page hit in browser. I found the httpcontext.current.session value is null.

if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            try
            {
                if (HttpContext.Current.Session.Keys.Count > 0)
                {

                }
                else
                {
                    HttpContext.Current.Response.Cookies["ecm"].Expires = DateTime.Now;
                    HttpContext.Current.Response.Cookies["ecmSecure"].Expires = DateTime.Now;


                }
            }
            catch (Exception ex)
            {

            }
        }

web.config session settings :

<sessionState mode="InProc" 
              stateConnectionString="tcpip=127.0.0.1:42424" 
              sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" 
              cookieless="false" 
              timeout="5" />
7
  • This will probably be helpful Commented Sep 17, 2013 at 17:54
  • Sorry jonesy. It isn't help full Commented Sep 17, 2013 at 18:02
  • What exactly means "loaded initially" ? Is that webforms or MVC asp.net app ? How session is declared in web.config ? Commented Sep 17, 2013 at 18:34
  • <sessionState mode="InProc" stateConnectionString="tcpip=127.0.0.1:42424" sqlConnectionString="data source=127.0.0.1;Trusted_Connection=yes" cookieless="false" timeout="5" /> Intially loaded the httpmodule.cs then we access the session there. Commented Sep 17, 2013 at 18:36
  • So initially really means in BeginRequest of HttpModule ? Commented Sep 17, 2013 at 18:44

2 Answers 2

0

One possible way could be to wrap it in a property and access it like a string:-

string SessionVariable
{
   get{
      return Session("SessionVariable") ?? String.Empty;
   }
   set{
      Session("SessionVariable") = value;
   }
}

then use like:-

if( String.IsNullOrEmpty( SessionVariable) )
{
   // do something
}
Sign up to request clarification or add additional context in comments.

3 Comments

sorry rahul. I didn't use web service. I use seesion.cs file in app code directory.
And what about IRequiresSessionState ??
this is not supported...any way
0

Thanks friends with responses. I found the answer. The session value is generated http module. only the end request is called after access the session value. The below code i used.

public void Init(HttpApplication context)
{
    context.BeginRequest += new EventHandler(context_BeginRequest);
    context.EndRequest += new EventHandler(context_EndRequest);
    context.AcquireRequestState += new EventHandler(SessionClear);
}

public void SessionClear(object sender, EventArgs e)
{
    try
    {
        if (HttpContext.Current != null && HttpContext.Current.Session != null)
        {
            if (!(HttpContext.Current.Session.Keys.Count > 0))
            {

            }

        }
    }
    catch (Exception ex)
    {
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.