0

Im using outproc session that is managed by aspnet_state. Sometimes I get run time errors saying that the session is invalid. So I wanted to check if the session is valid for every request I make. But I couldn't find a proper way to do it like in this Question using Java.

Here is the code I'm using right now in the page_preinit event.It looks ugly but it works.

            Try
                Dim x = Session.Keys().Item(0)
            Catch ex As Exception
                Session.Clear()
            End Try

Does any one knows a better approach?

Thanks

Community
  • 1
  • 1
Youssef
  • 728
  • 2
  • 10
  • 22
  • What's the exact runtime message you get? It doesn't ring a bell and the way you are testing with above work on the basis of a null reference exception which would also happen for a new/empty session instead of a invalid one. – olle Aug 28 '09 at 21:56
  • Are you using forms authentication? – PortageMonkey Aug 28 '09 at 23:32
  • olle: the session does exist (NOT NULL) but accessing any element throw an exception. This only happen from time to time and only on my dev machine as i do lot of build/rebuild – Youssef Aug 31 '09 at 15:59
  • PortageMonkey: no I'm not using from authentication – Youssef Aug 31 '09 at 16:05

3 Answers3

1

You could try checking the context object.

C#

if(Context.Session != null)
{
    //Redirect to login page etc
}

VB (Used a C# to VB converter here..not sure if this is correct)

If Context.Session IsNot Nothing Then
    'Redirect to login page etc
End If

If you are using forms authentication and it is setup correctly, it should redirect for you. If you still need to be explicit, I would recommend placing this type of code in the Global.asax, or a base class that each of your pages could derive from, rather than adding it to every request.

PortageMonkey
  • 2,675
  • 1
  • 27
  • 32
  • The code I'm using is on the base page. But the session does exist it is not null. Accessing eny element will throw an exception. By simply restarting the asp_net session the problem goes or in the code I clean it. The code I'm using does solve my problem but Iwas wondering if there is other to do it. – Youssef Aug 31 '09 at 16:03
  • Hmm. this sounds like you have a problem with the session information (keys) not being populated correctly...rather than the session timing out. We have had similar problems using the aspnet_state DB. Our approach has been to attempt to access a session key in addition to checking for null in order to validate that the aspnet_state DB returned a valid session object. As to the underlying problem with a bad session being returned by aspnet_state DB...still have yet to solve that one – PortageMonkey Aug 31 '09 at 17:27
0

Actually I solved the problem by checking the first element of the session( session.item(0) ) I put the code in try catch . if the there is an exception I just clear the session. This code is in a basepage for all my pages.

so in the preinit event my code look like this

Private Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
    Try
        Dim x = Session.Keys().Item(0)
    Catch ex As Exception
        Session.Clear()
    End Try
End Sub

It seems to be ugly but it does solve my problem.

Thanks everyone.

Ahmad Mageed
  • 94,561
  • 19
  • 163
  • 174
Youssef
  • 728
  • 2
  • 10
  • 22
0

Just check the Session.Count instead of attempting to access the first item in an error handling block...

dudeNumber4
  • 4,217
  • 7
  • 40
  • 57
  • No, the count did not help as the count was correct.The session has elements in it, but they are corrupted for some reason. You can replicate that by using out proc session after it runs for some times(heavily accessed) it will start crashing. It could be memory problem.I didn't investigate it throughly but the try catch works fine for me. – Youssef Nov 12 '09 at 15:53