Asked by:
Session variable access/update in generic handler.

Question
-
User-603743365 posted
Hi All,
I have a page on which there is a link, when clicked it makes a server call to handler using jquery ajax to start zip creation of a list of files. that click also strats a repeatative server calls which checks what is the current zip creation process.
The logic is simple, I have stored totalBytes in session which is sum of size of all the files, and I have another variable named processedBytes in the session which holds sum of files processed, I make server call every second and get (processedBytes / totalBytes * 100) from session which is percentage of process completed. but in current implementation it always returns 0 until and unless the zip creation is completed.
The zip creation method and the zip progress method are in the same handler, differently called using different paramters.
I don't know why it is not working. I have the same implementation in java and php they both seem to be working fine.
Thanks in advance.
Sunday, May 12, 2013 12:56 PM
All replies
-
User551462331 posted
http://www.hanselman.com/blog/GettingSessionStateInHttpHandlersASHXFiles.aspx
hope this helps...
Sunday, May 12, 2013 3:43 PM -
User-603743365 posted
Thanks for the reply.
That I know I added IRequiredSessionState in my Handler but the problem is when I access the variables. Here is some example shortened code.
<%@ WebHandler Language="VB" Class="Handler" %> Imports System Imports System.Web Public Class Handler : Implements IHttpHandler, IRequiresSessionState Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest 'method call depeding on parameter received from browser If (context.Request.Params.Get("method").Equals("createZip")) Then CreateZip(context) Else ZipProgress(context) End If End Sub Public Sub CreateZip(ByRef context As HttpContext) ' assume that this is populated with some file paths Dim files As ArrayList = New ArrayList context.Session("totalBytes") = 2048 'this value will be populated by iterating through all the files and adding size context.Session("processedBytes") = 0 'will loop through files and add them into a zip For Each file As String In files 'code to add individual file in a zip context.Session("processedBytes") = context.Session("processedBytes") + 1 ' this value is size of current file added to the zip Next End Sub Public Sub ZipProgress(ByRef context As HttpContext) 'this is processed bytes percentage of total bytes context.Response.Write(context.Session("processedBytes") * context.Session("totalBytes") / 100) End Sub End Class
In this code let's assume that ZipCreate method takes about 15 minutes to zip all the files but it keeps updating the processedBytes as soon as it adds a file in the zip with it's size.
I will keep calling the ZipProgress using ajax every second to check the progress of zip creation by checking the processedBytes session variable. that is where the problem occures if I debug processedBytes variable in ZipCreate method it prints correct value but if I print same in ZipProgress method it always prints 0. so the method always returns progress as 0 untill the zip progress method is completed.
I could send you the original code if you want.
Wednesday, May 15, 2013 11:48 AM -
User1109032460 posted
Bear in mind that the Session object is not updated until after the handler completes its work - at least in non-inproc modes. Remember, the point of IRequireSessionState is to indicate to the ASP.NET pipeline that it needs to save session state after the handler has done its stuff.
Therefore, there is no point coming back and calling ZipProgress until CreateZip has completed it's work.
Thursday, June 20, 2013 6:30 PM