处理自定义 SessionStateStores 的第二个答案。在查看代码时,我看不出它为什么不调用会话启动,所以我尝试创建自己的兼容但非功能性会话状态提供程序以查看发生了什么。使用我的会话状态提供程序,它在 global.asax 中调用 Session_start 很好。我将我的代码作为概念的演示证明:
会话状态提供者:
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;}
}
}
全球.asax:
protected void Session_Start(object sender, EventArgs e)
{
throw new Exception("It worked!");
}
网络配置:
<sessionState mode="Custom" customProvider="MySessionProvider">
<providers>
<add name="MySessionProvider" type="WebApplication1.SessionStateProvider"/>
</providers>
</sessionState>
这篇文章的重点主要是说,如果您使用提供的会话管理模块,即使使用自定义提供程序,它仍然应该触发 session_start 事件。也许您可以尝试使用我的虚拟状态提供程序,看看是否会触发您的事件。我还假设您的 session_onstart 的签名实际上是正确的?(即没有参数或 (object,eventargs) )。
目前我们处于一个愚蠢的位置,因为到目前为止我们所拥有的所有信息都是错误的。我们现在留下了有关您的 sessionstateprovider 导致 session_start 不触发或您的其余代码的某些内容。使用我的假人应该可以帮助我们回答这个问题(我希望如此)。
对于它的价值, session_start 事件应该在该CreateNewStoreData
方法在您的提供程序类中运行后立即被触发,因此您可以尝试在那里放置一个断点,然后查看它在该方法之后的位置。