0
votes

I've got a problem with HttpContext.Current.Session. I try to enable session state on my application. I did a few things:

  1. add to my web.config:
<sessionState 
   cookieless="UseCookies"
   regenerateExpiredSessionId="true"
   mode="InProc"
   timeout="20"/>

I want to use cookies too so I set up cookieless="UseCookies".

After that I added:

<pages enableSessionState="true">

this page section isn't closed because contains a few namespaces like that:

<pages enableSessionState="true">
 <namespaces>
   <add namespace="..."/>
 </namespaces>
</pages>
  1. In my Global.asax.vb I created two event handlers:
Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session is started
End Sub

and

 Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
    ' Fires when the session ends 
 End Sub
  1. I created some c# class which will be management my session:
 public class MySession
 {
    public static void Add(string key, object value)
    {
        if (HttpContext.Current.Session != null && (HttpContext.Current.Session.Keys == null || (HttpContext.Current.Session.Keys != null && HttpContext.Current.Session[key] == null)))
        {
            HttpContext.Current.Session.Add(key, value);
        }
    }

    public static void Remove(string key)
    {
        ...
    }

    public static void Update(string key, object value)
    {
        ...
    }

    public static object GetValue(string key)
    {
        ...
    }
}

It's all what I did in code and yeah - my app using vb and c# too. So when I try to use MySession.Add(someKey, someValue) always I've got a HttpContext.Current.Session == null.

It's my first time when I try turn on a session in app so I need help with this. I was looking for any solution into google but they either didn't work or they were not for me.

Any idea?

1
Can you capture the stack trace when the error occurs? - tahasozgen
I haven't any error just first condition of if statement on Add method is false because HttpContext.Current.Session == null. I thought that if I turn on session state in web.config and put enableSesionState in pages section - the session will be automatic created. - Presto
In Session_Start event have you checked HttpContext.Current.Session != null is true or false? - Mohsin Mehmood
ok I found something. When I implemented Application_AcquireRequestState event handler. It's void a few times - once Session isn't null nextly is null. - Presto
@MohsinMehmood in Session_Start event HttpContext.Current.Session isn't null - Presto

1 Answers

0
votes

I found the answer how to solve my problem. I have to use something like Pipeline (on proper place in code with proper code logic).

More informations are on this on StackOverflow link or on direct link on Microsoft Docs.