如何从自定义 HTTP 会话状态模块触发 Global.asax 的 Session_Start 事件?

Meh*_*hdi 5 .net c# asp.net session session-state

我编写了一个 HTTP 会话状态模块来处理我的自定义会话状态提供程序。
您知道Session_Start并将Session_EndInProc模式下工作,而不是在自定义模式下工作。
所以我希望 Global.asax 处理一个Session_Start方法(使用我的自定义会话模块和提供程序)以将它提升到我的应用程序上的一些初始化。

我发现这篇文章Sessoin_End在 StateServer 模式下处理事件的,但是处理开始事件呢?!

我的模块的开始事件:

public sealed class MyCustomSessionStateModule : IHttpModule
{
    ...

    /*
     * Add a Session_OnStart event handler.
     */
    public static event EventHandler Start
    {
        add
        {
            _sessionStartEventHandler += value;
        }
        remove
        {
            _sessionStartEventHandler -= value;
        }
    }

    ...
}
Run Code Online (Sandbox Code Playgroud)

我的 Web.config 模块配置:

<httpModules>
    <remove name="Session"/>
    <add name="MyCustomSessionStateModule" type="CustomSessionStateServer.MyCustomSessionStateModule" />
</httpModules>
Run Code Online (Sandbox Code Playgroud)

我的 Web.config 提供程序配置:

<sessionState  mode="Custom"  customProvider="CustomSessionStateStoreProvider" timeout="20">
    <providers>
        <add name="CustomSessionStateStoreProvider" type="CustomSessionStateServer.CustomSessionStateStoreProvider" />
    </providers>
</sessionState>
Run Code Online (Sandbox Code Playgroud)

我在一个结构中编写了我的模块, session_start 将在 AcquireRequestState 的开始时间触发。

/*
 * 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

根据ASP.net 应用程序生命周期,会话状态必须在AcquireRequestState事件发生时可用。但我在 Global.asax 的 Session_Start 中仍然有空会话对象。
我根据微软的 .NET 4.5 框架 SessionStateModule Source Code编写了这个模块。