Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Last post Jul 30, 2011 09:52 AM by proovit.com
Member
6 Points
3 Posts
Jul 30, 2011 09:52 AM|LINK
VB Code (not tested, see converter.telerik.com)
Imports System
Imports System.Web
Imports System.Web.SessionState
Imports System.Diagnostics
' This code demonstrates how to make session state available in HttpModule,
' regradless of requested resource.
' author: Tomasz Jastrzebski
' Vb: Erik van der Heide (not tested yet)
Public Class MyHttpModule
Implements IHttpModule
Public Sub Init(application As HttpApplication) Implements IHttpModule.Init
AddHandler application.PostAcquireRequestState, AddressOf Application_PostAcquireRequestState
AddHandler application.PostMapRequestHandler, AddressOf Application_PostMapRequestHandler
End Sub
Private Sub Application_PostMapRequestHandler(source As Object, e As EventArgs)
Dim app As HttpApplication = DirectCast(source, HttpApplication)
If TypeOf app.Context.Handler Is IReadOnlySessionState OrElse TypeOf app.Context.Handler Is IRequiresSessionState Then
' no need to replace the current handler
Return
End If
' swap the current handler
app.Context.Handler = New MyHttpHandler(app.Context.Handler)
Private Sub Application_PostAcquireRequestState(source As Object, e As EventArgs)
Dim resourceHttpHandler As MyHttpHandler = TryCast(HttpContext.Current.Handler, MyHttpHandler)
If resourceHttpHandler IsNot Nothing Then
' set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler
' -> at this point session state should be available
Debug.Assert(app.Session IsNot Nothing, "it did not work :(")
Public Sub Dispose() Implements IHttpModule.Dispose
' a temp handler used to force the SessionStateModule to load session state
Public Class MyHttpHandler
Implements IHttpHandler
Implements IRequiresSessionState
Friend ReadOnly OriginalHandler As IHttpHandler
Public Sub New(originalHandler__1 As IHttpHandler)
OriginalHandler = originalHandler__1
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
' TODO: Implement this property getter
' IsReusable must be set to false since class has a member!
Throw New NotImplementedException()
End Get
End Property
Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
' do not worry, ProcessRequest() will not be called, but let's be safe
Throw New InvalidOperationException("MyHttpHandler cannot process requests.")
End Class
proovit.com
Member
6 Points
3 Posts
Re: SessionState in HttpModule problem (2.0)
Jul 30, 2011 09:52 AM|LINK
VB Code (not tested, see converter.telerik.com)
<div class="WordSection1">Imports System
Imports System.Web
Imports System.Web.SessionState
Imports System.Diagnostics
' This code demonstrates how to make session state available in HttpModule,
' regradless of requested resource.
' author: Tomasz Jastrzebski
' Vb: Erik van der Heide (not tested yet)
Public Class MyHttpModule
Implements IHttpModule
Public Sub Init(application As HttpApplication) Implements IHttpModule.Init
AddHandler application.PostAcquireRequestState, AddressOf Application_PostAcquireRequestState
AddHandler application.PostMapRequestHandler, AddressOf Application_PostMapRequestHandler
End Sub
Private Sub Application_PostMapRequestHandler(source As Object, e As EventArgs)
Dim app As HttpApplication = DirectCast(source, HttpApplication)
If TypeOf app.Context.Handler Is IReadOnlySessionState OrElse TypeOf app.Context.Handler Is IRequiresSessionState Then
' no need to replace the current handler
Return
End If
' swap the current handler
app.Context.Handler = New MyHttpHandler(app.Context.Handler)
End Sub
Private Sub Application_PostAcquireRequestState(source As Object, e As EventArgs)
Dim app As HttpApplication = DirectCast(source, HttpApplication)
Dim resourceHttpHandler As MyHttpHandler = TryCast(HttpContext.Current.Handler, MyHttpHandler)
If resourceHttpHandler IsNot Nothing Then
' set the original handler back
HttpContext.Current.Handler = resourceHttpHandler.OriginalHandler
End If
' -> at this point session state should be available
Debug.Assert(app.Session IsNot Nothing, "it did not work :(")
End Sub
Public Sub Dispose() Implements IHttpModule.Dispose
End Sub
' a temp handler used to force the SessionStateModule to load session state
Public Class MyHttpHandler
Implements IHttpHandler
Implements IRequiresSessionState
Friend ReadOnly OriginalHandler As IHttpHandler
Public Sub New(originalHandler__1 As IHttpHandler)
OriginalHandler = originalHandler__1
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
' TODO: Implement this property getter
' IsReusable must be set to false since class has a member!
Throw New NotImplementedException()
End Get
End Property
Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest
' do not worry, ProcessRequest() will not be called, but let's be safe
Throw New InvalidOperationException("MyHttpHandler cannot process requests.")
End Sub
End Class
End Class
</div>