I wrote a HTTP Session State Module that handles my custom Session State Provider.
And you know Session_Start
and Session_End
will work in InProc
mode and not in Custom mode.
So I want Global.asax to handle a Session_Start
method (with my Custom session module and provider) to raise it to some initialisations on my application.
I found this Article to handle Sessoin_End
Event in StateServer mode, but what about handling the Start Event?!
My Module's Start Event:
public sealed class MyCustomSessionStateModule : IHttpModule
{
...
/*
* Add a Session_OnStart event handler.
*/
public static event EventHandler Start
{
add
{
_sessionStartEventHandler += value;
}
remove
{
_sessionStartEventHandler -= value;
}
}
...
}
My Web.config Module Configurations:
<httpModules>
<remove name="Session"/>
<add name="MyCustomSessionStateModule" type="CustomSessionStateServer.MyCustomSessionStateModule" />
</httpModules>
My Web.config Provider Configurations:
<sessionState mode="Custom" customProvider="CustomSessionStateStoreProvider" timeout="20">
<providers>
<add name="CustomSessionStateStoreProvider" type="CustomSessionStateServer.CustomSessionStateStoreProvider" />
</providers>
</sessionState>
I wrote my module in a structure that the session_start will fire in AcquireRequestState's Begin Time.
/*
* IHttpModule Member
*/
public void Init(HttpApplication app)
{
...
// Handling OnAcquireRequestState Asynchronously
app.AddOnAcquireRequestStateAsync(
new BeginEventHandler(this.app_BeginAcquireState),
new EndEventHandler(this.app_EndAcquireState));
...
}
private IAsyncResult app_BeginAcquireState(object source, EventArgs e, AsyncCallback cb, object extraData)
{
...
if (_rqIsNewSession)
{
// Firing Sessoin_Start Event
_sessionStartEventHandler(this, EventArgs.Empty);
}
}
According to ASP.net Application Life Cycle The Session State must be available on AcquireRequestState
event. but still i have null Session object in Gloabal.asax's Session_Start.
I wrote this module according to the Microsoft's .NET 4.5 framework SessionStateModule Source Code.