0

Need help on setting and getting object from session in asp.net web services. I am saving session in one method of service and when try to retrieve it , just gives null. Sample code is bellow for saving session

 [WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string setObj(NameValue[] formVars)
    {
       MyObject obj = new MyObject();
       obj.vlaue = "1";
       Session["obj"] = (object) obj;
        return new JavaScriptSerializer().Serialize("");
     }

For getting session :

[WebMethod(EnableSession = true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string getObj(NameValue[] formVars)
    {
       MyObject obj = new MyObject();

        obj = (MyObject)Session["obj"] ;
        return new JavaScriptSerializer().Serialize(obj.value);
     }

In above code session data is null. I have also add session state in web config but won't work:

<system.web>

 <pages>
  <namespaces>
    <add namespace="System.Web.SessionState"/>
  </namespaces>
</pages>
<sessionState mode="InProc"></sessionState>

Thanks.

1 Answers1

0

I am saving session in one method of service and when try to retrieve it , just gives null.

If these 2 methods are during the same request, it will be null. a session variable in .Net is set on the client side in the response headers, but when you try to read them they are from the client's request headers, which have not changed since the request was made.

Eric Lizotte
  • 224
  • 1
  • 7