locked
Accessing Session from AsyncCallback? RRS feed

  • Question

  • User845401972 posted

    Is there a way to access the Session state (HttpContext.Current.Session) from an AsyncCallback method?

    I understand now that it's because the asynch process runs on a different thread than the session or UI thread.  However, I wasn't sure if there was a way to work around this.  Thanks.

    Friday, October 12, 2012 10:07 AM

Answers

  • User1779161005 posted

    I don't think you'll have too many threading problems if you're writing the callback code and you can coordinate how many concurrent instances are running (i'm guessing usually only one). My concern is the use of session in there -- normally asp.net throttles requests for the same session ID making them run in sequence. they do this so you don't have to worry about 2 pages/controllers from updating session in parallel and so you don't have to think about locking.

    Actually, what am i thinking -- from ASP.NET's perspective your request is not done, therefore the session will still be locked and no other requests for that session will be allowed in. You can forget about my warning with session -- you'll be fine.

    Except you'll have all your request throttled because you're using session, but this is unrelated to the async stuff.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2012 11:36 AM

All replies

  • User1779161005 posted

    When doing async programming in ASP.NET, the "Begin" method will have the HttpContext.Current (and thus session, etc), but the "End" will not. It's up to you to marhal the reference to the callback method. Beware -- the ASP.NET plumbing doesn't know you're doing this and therefore might not provide its normal concurrency control for session, so make sure you test it well :)

    Friday, October 12, 2012 10:30 AM
  • User845401972 posted

    Thanks, I tested that I can create a class that with a property of type HttpContext to pass the context in the IAsyncResult.AsyncState.  However, I'm not sure if this will cause threading or performance issues by passing the HttpContext around this way. 

    Do you have thoughts on this technique?  Thanks again.

     

           


     

    Friday, October 12, 2012 11:15 AM
  • User1779161005 posted

    I don't think you'll have too many threading problems if you're writing the callback code and you can coordinate how many concurrent instances are running (i'm guessing usually only one). My concern is the use of session in there -- normally asp.net throttles requests for the same session ID making them run in sequence. they do this so you don't have to worry about 2 pages/controllers from updating session in parallel and so you don't have to think about locking.

    Actually, what am i thinking -- from ASP.NET's perspective your request is not done, therefore the session will still be locked and no other requests for that session will be allowed in. You can forget about my warning with session -- you'll be fine.

    Except you'll have all your request throttled because you're using session, but this is unrelated to the async stuff.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, October 12, 2012 11:36 AM