views:

61

answers:

3

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

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.

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.

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.

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 >

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.

+1  A: 

Edit: Looks like I did miss the point on the question slightly. I'm going to leave this answer here though just because it may be of interest to some. Feel free to comment if you think it should be deleted though.

Sorry this is an answer since I'm not 100% sure of what I'm saying but it got too long to be a comment...

I wasn't sure if you had created a custom session module or jsut a session state store provider. I'm assuming the module but I wasn't sure from just "SessionStateProvider"...

From what I can tell any httpmodule added in the web.config will go through a process of trying to hook up to events in the global.asax. The way it does this seems to be by looking for methods of the form "{name}_{event}" or "{name}_on{event}" and wiring them up where I think that {name} is the name you gave the module in your web.config and {event} of course is the name of the event in the class (ie start).

So in essence if your new module is included like this:

<add name="MySession" type="MySessionStateModule" />

then you would want in your global.asax MySession_Start

I hope this helps. Sorry if it doesn't and its all stuff you've tried before. I'd suggest giving a bit of code and some ideas of what you have already tried and that fails though. Code that might be interesting is where you add your custom code into the site (ie the appropriate part of web.config) and maybe some code in your session class if it can be easily cut down to a trivial form (eg a session provider that just returns a constant could be short and demonstrate your error). Also some indication of whether it is keeping session state or not (ie does it work apart form not calling onstart) would be good. :)

Edit to add: I thought I'd link one of the useful web pages I came across. http://aspnetresources.com/articles/event_handlers_in_global_asax talks about how standard error handlers are wired up in the global.asax and pointed me at the interesting method HookupEventHandlersForAppplicationAndModules that gave me a clue as to how some of this might be working.

Chris
I've tried adding a sub called MySession_OnStart, but that doesn't seem to be getting called.
Rice Flour Cookies
A: 

I'm going to guess the operational goal is to do something like, say, initialize the session rather than needing to specifically do something in the Session_OnStart method. Presuming this is the case, I think you can do the following:

1) Handle the Appication.PostAcquireRequestState event.
2) In handler, check if the session is initialized--poor man's way is to set a variable in the session--then act appropriately.

Wyatt Barnett
+1  A: 

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:

Session State Provider:

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:

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

web.config:

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

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) ).

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).

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.

Chris
Great answer Chris. After reading your answer, I discovered that the problem seems to have to do with the `SessionIDManager`, not the `SessionStateProvider`.
Rice Flour Cookies
@Rice Flour Cookies: Cool. Was good fun finding the answer for you. Did you actually find the problem with your SessionIDManager or is there another question coming up? And selfishly don't forget to accept my answer so I can get the bounty. ;-)
Chris
Well, I'm hoping to find out how to get the event raised using the custom `SessionIDManager`.
Rice Flour Cookies
@Rice Flour Cookies: I can't actually see what the SessionIDManager has to do with the raising of this event... Could you explain how you narrowed it down to that being at fault? and do you have a custom SessionStateModule or are you using the standard one with various additions in supporting classes? The event is raised as standard in SessionStateModule in the CompleteAquireState method. If you're using this standard one then I can't really see how it can not be getting called...
Chris
@Chris, I narrowed it down to the `SessionIDManager` by simply removing `sessionIDManagerType="SessionIDManager.ProgramSessionIDManager"` from my web.config file
Rice Flour Cookies
I've been looking at how that onstart is called... With my dummy code it gets called once on startup and if I change the GetItem methods to return null instead of a sessionStateStore object then it assumes that the session is new and calls my onstart again. I'm not really sure how the SessionIDManager ties in but it occurs to me that if it is generating IDs that are not user specific such that it is somehow always returning a valid session state then it will never be calling that new session code. The only thing is that this would still be calling it on the first startup as far as I can see...
Chris
@Chris, I'm using a framework where a user logs in to my program using a custom logon application. The logon application initiates the user's session and stores it in a special database. So, even for the first request to my web app, the session technically already exists... However, I would like to see `Session_OnStart` called the first time a user fetches a page. Also, it is even more important that `Session_OnEnd` be called. I want `Session_OnEnd` to run some special code to log the user out and direct him to another page.
Rice Flour Cookies
I take it that you can't just put your Session_start code in your logon app where it creates the session or create an event there of some kind? And also wherever you determine that a session has ended coudl you throw your own event there? I'm startign to clutch at straws now I have to admit but it strikes me if you are circumventing to a certain extent the way the main Session object expects things to work you are goign to have to throw some of your own events... (not to say what you are doing is wrong necessarily, jsut that it is different from teh default inproc provider).
Chris
to be entirely honest, handling `Session_OnEnd` is more important to me than handling `Sesion_OnStart`. I just mentioned Session_OnStart in my question because it happens first. As we have discovered, it looks like `Session_OnStart` doesn't execute because the session has already been set up outside the web application. Interestingly, if I call `Session.Abandon` during a page load, I would have expected that `Session_OnEnd` would be called, but much to my surprise, `Session_OnEnd` does not run; but `Session_OnStart` runs on the next page load!
Rice Flour Cookies
As I mentioned in a comment on the question itself the OnEnd apparently only works for the inproc provider. I'm not sure why this is and haven't found anywhere near as much useful clues as to how the End event is called. I'll try to take a look tomorrow if I get a chance. Or maybe later tonight if work needs a break. :)
Chris
+300 Thanks, Chris. Although this does not answer my question directly, it gave me a push in the right direction. Very helpful!
Rice Flour Cookies
@Rice Flour Cookies: Cool. If you find yourself asking any more related questions I'd be interested to have a look. You've got me quite interested in the workigns of custom session providers now. ;-) I guess just commenting here is the best way to notify me. Though I offer no guarantees of being able to answer them. ;-)
Chris