referencing application object without httpContext RRS feed

  • Question

  • User1659395126 posted

    Hi all,

    I am sure this must be possible but have so far drawn a blank.

    I'm storing a list of logged on users in the sites application state object. There is also some user specific information stored in the cache, this info has a cacheItemRemovedCallback which closes the session on the database.

    I'm trying to get the cacheItemRemovedCallback to also update the users table stored in the application state and have tried various ways which gives various errors:

    1 - httpContext.current.application("usersTable").. do stuff..

    ..this compiles ok but gives an error ( NullReferenceException ) when cacheItemRemovedCallback fires.. I realised this is because since it's triggered by cacheItemRemovedCallback then there isn't an httpContext to reference and so can't access the application.. fair enough and to be expected!

    2 - application("usersTable").. do stuff.. This fails to compile since 'Reference to a non-shared member requires an object reference'.. again, to be expected.

    So my question is, is there a way I can access the application object without an httprequest to reference.

    I've trawled google and various forums but no luck so any input would be great!



    Thursday, November 24, 2016 6:48 PM

All replies

  • User1659395126 posted

    After another few hours googling and hacking I've come up with a workaround. It's a bit of a dirty hack but it works.

    The solution is to define a module with a single variable declaration..

    public module mySiteContext

        public application as httpApplicationState
    end module

    Then set this property during application_start in global.asax


    You can then reference the application from anywhere replacing httpContext.Current.application with mySiteContext.application

    As I said it's a hack but it seems to work.

    If anyone has a better solution then please let me know.


    With thanks to Culme in this post https://forums.asp.net/t/1436067.aspx?Application+Variable+in+thread+HttpContext+is+null

    Friday, November 25, 2016 12:28 AM
  • User1324658857 posted


    As far as I know, Application state is not shared between multiple servers within a Web farm or between worker processes in a Web garden. 

    Therefore, application state is a useful place to store small amounts of often-used data that does not change from one user to another. 

    In my opinion, we could directly access the application state in the application, why you use httpContext?

    Monday, November 28, 2016 7:38 AM
  • User1659395126 posted

    Thanks for the reply.


    ..why you use httpContext?

    My problem was trying to get a reference to the application without a request to give it context.. ie, if you are trying to access application from a callback function.

    the only way I've found so far is as mentioned above.

    Tuesday, November 29, 2016 1:54 PM