Click here to Skip to main content
15,896,286 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
I created an HttpHandler class in ASP.NET and configured a website to handle any request with the *.test path.

public class GameHandler : IHttpHandler, IRequiresSessionState
{
    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
    public void ProcessRequest (HttpContext context)
    {
        if (context.Request.ContentType == "application/json; charset=utf-8")
        {
            ...
            switch (parameters ["type"])
            {
                case "Setup":
                    result = Setup (context);
                    break;
                case "DoStep":
                    result = DoStep (context, parameters);
                    break;
             }
             ...
         }
         else
             context.Response.Write (@"
                  <html>
                      <head>
                      </head>
                      <body>
                          <!-- some HTML -->
                      </body>
                  </html>"); // this is returned on first request
    }


In the Setup method I have such a code:
context.Session ["Game"] = new Game ();
In the DoStep method however, context.Session.Count = 0 and context.Session["Game"] is NULL. I figured out that context.Application also loses the values that I put in it. In client side I use jquery to call these functions. Such a call looks like this:

$.ajax({
       url: "/test.test",
       type: "POST",
       data: "{'type':'Setup'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (result) {...}
});
$.ajax({
       url: "/test.test",
       type: "POST",
       data: "{'type':'DoStep','row':'" + row + "','column':'" + column + "'}",
       contentType: "application/json; charset=utf-8",
       dataType: "json",
       success: function (result) {...}
});


I suspect the problem is that ASP.NET doesn't know these requests sent from javascript belong to the same session and that's why the Session values are lost. I think that I would need to send back some cookie information or something for the next request to be identified but the fact is I don't have any idea.

Any help is really appreciated.
Posted
Updated 10-Mar-11 23:10pm
v2

1 solution

Hello,

Do you access a ASP.NET Page (from the same ASP.NET application as the service is working on) before you execute the ajax requests?

If yes, try to save anything in the session before you deliver the page (for example a userId / GUID or if you do not need any useful data, save the current DateTime). This should make the session id permanent and usable for the ajax calls / services.

Hope this helps :)

Best regards and have a nice day,
Stops
 
Share this answer
 
Comments
Zoltan Aszalos 11-Mar-11 7:04am    
Hi Stops,

No I don't access an ASP.NET page. I just type in http://localhost:83/test.test in the browser and that's all. I tried to save something in context.Session even at the first call to ProcessRequest but next time, the value was gone, and I still can't use context.Session.
What else could I try ?
Christoph Keller 11-Mar-11 7:13am    
Could you post the sessionState entry in your web.config, perhaps there is something wrong.

Also could you try just writing out the session id (Session.SessionId), is it changing on each request or is it permanently the same?
Zoltan Aszalos 11-Mar-11 8:59am    
This is from web.config: <sessionstate cookieless="false" mode="InProc" timeout="20">

I checked the session id and it is the same, it doesn't change on each request, but the values I store in Session are lost.
Christoph Keller 14-Mar-11 3:45am    
Hi again,

Have you allready tried to just put a string or a int into the session and check if this would work?
How is your Game class defined, is it marked [Serializable] (required for objects which will be hold in the session / cache)?

Perhaps you could post the Game class in the answer (just the accessor definitions, code should not be required)?
Zoltan Aszalos 14-Mar-11 4:42am    
Hi,

I tried to put a string value into the session:

var doc = XDocument.Load (@"c:\XO_Game_Website\bin\test.xml");

if (context.Session ["something"] == null)
{
context.Session.Add("something", "something");

doc.Root.Element ("xxx").Value = "null";
}
else
{
doc.Root.Element ("xxx").Value = "not null";
}

Every time the "null" text value is written into the file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900