Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / October 2006

Tip: Looking for answers? Try searching our database.

session variable and asynchrous call

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
carrolky@gmail.com - 26 Oct 2006 23:48 GMT
Here's my scenario.  Please advise me if you can.

I have an ASP.NET web application on a corporate intranet which
retrieves some user information it uses from a webservice.  The data
does not change often, but when it does, my site needs the updated
information.  The connection to the database is slow and occasionally
unreliable.  To avoid problems, we are storing the needed information
in cookies that do not expire for over 10 years.  We use the cookie
values in the absence of "better information".  Periodically we'd like
to update the cookie values.  Our approach is to include an additional
cookie that expires weekly.  When a user returns to our site, we check
for the presence of this cookie, if it is not present (new user or
expired cookie) we need to query the webservice.  This can take long to
return (or may timeout), and we cannot make our users wait.  BTW, all
of this processing occurs in a PreRequestHandlerExecute event handler
method of an HTTP module.

In order to refresh the data without waiting, I would like to call the
web service asynchronously.  I have written the code to use an
HttpWebRequest object and call the BeginGetRequestStream method.  I
have a callback function that parses the responsestream and extracts
the data I need.  I need to put this data into cookies.  I can wait
until the next request/response cycle.

My question is this:  Since the callback function may not be invoked
until after the response has been streamed to the client, where can I
put the information I've retrieved?  I've tried adding a reference to
the current session to my state object.  I then store the information
in Session variables using the state object.  This works while the cal
back function is executing (the values exist in session variables).
The values are gone when the next request cycle is processed.

Am I doing something wrong?  Is this not a viable approach?  If not,
where can I store this data in the case that the response has already
been streamed to the client?

Again, any advice would be appreciated.
Bryan Phillips - 27 Oct 2006 02:43 GMT
You should look into caching your data in the ASP.Net Cache.  When you
add the item to cache, you can specify a duration and a method to call
when the cached value expires.  Then you can reload the data in the
background without affecting the users.

Bryan Phillips
MCSD, MCDBA, MCSE
Blog:  http://bphillips76.spaces.live.com

> Here's my scenario.  Please advise me if you can.
>
[quoted text clipped - 33 lines]
>
> Again, any advice would be appreciated.
carrolky - 27 Oct 2006 13:47 GMT
Bryan,

I am quite familiar with caching, but I'm not sure this will work.  The
data I'm retrieving is specific to each user.  This is why I'm choosing
cookies as my data store.  I can temporarily (until the next
request/response cycle) place these somewhere else, as long as I can
get back to the values in the next cycle.  In order to scope these to
the current user, session variables seemed like the logical answer, but
this doesn't appear to work under these conditions.  I'm willing to
entertain other solutions as well.

Again the heart of the problem is that the data retrieval may take a
while to complete.  I am requesting this data asynchronously so the
user is not impacted.  However the callback may not be invoked until
after the response has been streamed to the user.  Is there some place
I can store the data that will be in the user's scope on the next
request/response cycle?

> You should look into caching your data in the ASP.Net Cache.  When you
> add the item to cache, you can specify a duration and a method to call
[quoted text clipped - 42 lines]
> >
> > Again, any advice would be appreciated.
carrolky - 27 Oct 2006 19:14 GMT
Eureka!  I've got it.

Apparently there is a feature of ASP.Net such that if there is no value
stored in session state, there is nothing saved on the server.  This
makes sense when you think about it.  Why allocate storage space to a
session with nothing in it?  Apparently the decision to not store
values is made before my callback function.  Despite that I was able to
write values to session variables in the callback function, these were
not available when the next request cycle began.

I've read several articles that discuss this behavior under other
symptoms.  Typically those involved lost session variables.  In each of
those cases, the session id was different from one request/response
cycle to the next.  In my case, the session ID was the same every time,
but the variables were not present if the asynchronous callback
occurred after the response had been sent to the client.

The solution in this case was to add a dummy session variable just
prior to firing the asynchronous call (Application.Session["DUMMY"] =
String.Empty;).  This ensures that the session has some state
information and causes the decision to preserve the session state to be
true.  The acutal saving (I assume) does not occur until the session is
about to be destroyed.  When my callback function is invoked, I am able
to retrieve the reference to my session that I had stored in the
AsyncState.  I can then write my additional variables to the session
state.  Now however, these values are present when the next request
cycle begins.  I have additional code that checks for the presence of
these variables on the next request to this session.  If they are
present, they are added to cookies that will not expire for a week,
avoiding the need to repeat this lengthy process for some time.

Hope this helps someone.

> Bryan,
>
[quoted text clipped - 60 lines]
> > >
> > > Again, any advice would be appreciated.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.