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!
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.
None
0 Points
3 Posts
referencing application object without httpContext
Nov 24, 2016 06:48 PM|funky_fox|LINK
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!
Thanks,
Jeff
None
0 Points
3 Posts
Re: referencing application object without httpContext
Nov 25, 2016 12:28 AM|funky_fox|LINK
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
mySiteContext.application=httpContext.current.application
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.
Jeff
With thanks to Culme in this post https://forums.asp.net/t/1436067.aspx?Application+Variable+in+thread+HttpContext+is+null
Member
50 Points
55 Posts
Re: referencing application object without httpContext
Nov 28, 2016 07:38 AM|aliceyonng@outlook.com|LINK
Hi,
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?
None
0 Points
3 Posts
Re: referencing application object without httpContext
Nov 29, 2016 01:54 PM|funky_fox|LINK
Thanks for the reply.
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.