从定制ASP.NET SessionStateProvider类提高Session_OnStart事件 [英] Raise Session_OnStart event from custom ASP.NET SessionStateProvider class

查看:155
本文介绍了从定制ASP.NET SessionStateProvider类提高Session_OnStart事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个内部开发的定制SessionStateProvider类的asp.net项目。

I'm working on an asp.net project that uses an in-house developed custom SessionStateProvider class.

显然,Session_OnStart事件在不触发。我在Global.asax中code来处理该事件。我只能得到这个code,通过改变在Web.config使用默认SessionStateProvider执行。

Evidently, the Session_OnStart event is not firing. I have code in Global.asax to handle the event. I can only get this code to execute by changing the Web.config to use the default SessionStateProvider.

我必须做,使当我使用自定义SessionStateProvider类我的Session_OnStart code执行?我访问源$ C ​​$ C。

What must I do to make my Session_OnStart code execute when I'm using a custom SessionStateProvider class? I have access to the source code.

更新:上面清清楚楚地写着MSDN上,按说,当会话模式是InProc,所以我打算让的Session_OnStart 仅火灾做一些特别的东西让这个接线我希望的方式。

Update: It clearly says on MSDN that ordinarily, Session_OnStart only fires when the Session mode is InProc, so I'm going to have to do something special to get this wired up the way I want it.

在web.config中我的会话状态的配置是这样的:

My session state configuration in web.config looks like this:

< sessionState cookieless="false" regenerateExpiredSessionId="true" mode="Custom"  
 customProvider="ProgramSessionStateProvider"  
 sessionIDManagerType="SessionIDManager.ProgramSessionIDManager"  
sqlConnectionString="SqlSessionServices" cookieName="smartSessionID" >
< providers >
            < add name="ProgramSessionStateProvider"   
type="SessionStateProvider.ProgramSessionStateProvider"   
connectionStringName="SqlSessionServices" writeExceptionsToEventLog="false" / >  
        < /providers >
    < /sessionState >

再次更新:今天早上我发现了一些有趣的事情。读克里斯的回答后,我尝试使用刚刚顾客 SessionStateProvider 并取消了code自定义 SessionIDManager 。一旦我这样做,我立刻就能看到的Session_OnStart 方法执行。问题是,我的自定义 SessionStateProvider 需要的自定义 SessionIDManager 。凡恰恰是的Session_OnStart 事件烧制而成?它看起来像它与做 SessionIDManager ,而不是 SessionStateProvider

Update again: I found something interesting this morning. After reading Chris's answer, I tried using just the customer SessionStateProvider and removed the code for the custom SessionIDManager. Once I did that, I was immediately able to see the Session_OnStart method execute. The problem is, my custom SessionStateProvider requires a custom SessionIDManager. Where exactly is the Session_OnStart event fired from? It looks like it has to do with the SessionIDManager, not the SessionStateProvider.

推荐答案

一个第二个答案涉及定制SessionStateStores。在看code我看不到任何理由为什么它不会把会话开始,所以我试图创建自己的标准,但非功能性的会​​话状态提供者,看看发生了什么事情。随着我的会话状态提供它在Global.asax中精细称在session_start。我有我的code为概念的演示证明:

A second answer dealing with custom SessionStateStores. On looking at the code I could see no reason why it wouldn't call session start so I tried to create my own compliant but non-functional session state provider to see what was going on. With my session state provider it called the Session_start in global.asax fine. I include my code as a demo proof of concept:

会话状态提供程序:

namespace WebApplication1
{
    public class SessionStateProvider : System.Web.SessionState.SessionStateStoreProviderBase
    {
        public override SessionStateStoreData CreateNewStoreData(HttpContext context, int timeout)
        {
            ISessionStateItemCollection foo = new SessionStateItemCollection();
            HttpStaticObjectsCollection bar = new HttpStaticObjectsCollection();
            return new SessionStateStoreData(foo, bar, 300);
        }
        public override void CreateUninitializedItem(HttpContext context, string id, int timeout){}
        public override void Dispose() { }
        public override void EndRequest(HttpContext context) { }
        public override SessionStateStoreData GetItem(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            locked = false;
            lockAge = TimeSpan.FromSeconds(10);
            lockId = new object();
            actions = SessionStateActions.None;
            ISessionStateItemCollection foo = new SessionStateItemCollection();
            HttpStaticObjectsCollection bar = new HttpStaticObjectsCollection();
            return new SessionStateStoreData(foo, bar, 300);
        }
        public override SessionStateStoreData GetItemExclusive(HttpContext context, string id, out bool locked, out TimeSpan lockAge, out object lockId, out SessionStateActions actions)
        {
            locked = false;
            lockAge = TimeSpan.FromSeconds(10);
            lockId = new object();
            actions = SessionStateActions.None;
            ISessionStateItemCollection foo = new SessionStateItemCollection();
            HttpStaticObjectsCollection bar = new HttpStaticObjectsCollection();
            return new SessionStateStoreData(foo, bar, 300);
        }
        internal virtual void Initialize(string name, NameValueCollection config, IPartitionResolver partitionResolver) { }
        public override void InitializeRequest(HttpContext context) { }
        public override void ReleaseItemExclusive(HttpContext context, string id, object lockId) { }
        public override void RemoveItem(HttpContext context, string id, object lockId, SessionStateStoreData item) { }
        public override void ResetItemTimeout(HttpContext context, string id) { }
        public override void SetAndReleaseItemExclusive(HttpContext context, string id, SessionStateStoreData item, object lockId, bool newItem) { }
        public override bool SetItemExpireCallback(SessionStateItemExpireCallback expireCallback){return true;}
    }
}

Global.asax中:

Global.asax:

    protected void Session_Start(object sender, EventArgs e)
    {
        throw new Exception("It worked!");
    }

web.config中:

web.config:

    <sessionState mode="Custom" customProvider="MySessionProvider">
        <providers>
            <add name="MySessionProvider" type="WebApplication1.SessionStateProvider"/>
        </providers>
    </sessionState>

这篇文章的要点主要是说,如果你使用的是提供的会话管理模块,甚至自定义提供仍应关火的session_start事件。或许你可以用我的虚拟状态提供尝试,看看是否触发了您的活动。此外,我认为你的Session_OnStart的签名实际上是正确的? (即没有PARAMATERS或(对象,EventArgs的))。

The point of this post is mainly to say that if you are using the provided session management module, even with a custom provider it should still fire off the session_start event. Perhaps you can try with my dummy state provider and see if that fires off your event. Also I assume that the signature of your session_onstart is actually correct? (ie no paramaters or (object,eventargs) ).

目前,我们正处在一个位置,达夫因为所有我们至今的信息是沿着错误路线。我们现在只剩下要么讲讲你sessionstateprovider导致了在session_start不火或者一些有关你的code的其余部分。用我的假人应该有助于我们回答这个问题(我希望)。

Currently we are at a duff position because all the information we have so far is along the wrong lines. We are now left with either something about your sessionstateprovider is causing the session_start not to fire or something about the rest of your code is. Using my dummy should help us answer that question (I hope).

有关哪些公司值得在session_start事件应该只在 CreateNewStoreData 方法在你的供应商类已运行,所以你可以尝试把在一个断点,只是看到的地方后,被解雇它头部的方法之后。

For what its worth the session_start event should get fired just after the CreateNewStoreData method has run in your provider class so you can try putting in a breakpoint there and just seeing where it does head to after that method.

这篇关于从定制ASP.NET SessionStateProvider类提高Session_OnStart事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆