Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
In my web service I need to get one value from session variable. In the constructor of my web service I am getting this value like


C#
constructor()  // constructor of web service
{
  Person p = (Person)Session["xyz"];
  string name = p.Name;
  string Address = p.Address
}


my java script is like bellow

JavaScript
function CallWebservice()
{
MyWebservice.GetValueFromDb(onsucess,onfailure)
}


It works perfectly fine but some time i got the session as null, i have the session state in web.config which has default time out as 60 minutes.

but before 60 minutes(not all the time) the session is getting null, this things happens only when i am calling the webservice from java script
Posted
Updated 13-Feb-13 2:35am
v3
Comments
ZurdoDev 13-Feb-13 9:42am    
Do you have [WebMethod(EnableSession = true)] on your webmethod?

1 solution

Im agreed with ryanb31 answer.
First you have to enable the session in your web service.
you can do that by adding following line to your web service
[WebMethod(EnableSession = true)]

http://msdn.microsoft.com/en-us/library/aa480509.aspx[^]
http://msdn.microsoft.com/en-us/library/system.web.services.webmethodattribute.enablesession(v=vs.90).aspx[^]

and avoid the session time out, you can create a non functional web service and keep calling to it using the javascript setInterval method.
HTML
[WebMethod(true)]
        public void KeepAlive()
        {
            //do nothing
        }

JavaScript
//to keep the session alive
window.setInterval(function(){
  //call KeepAlive web service 
}, 60000);
 
Share this answer
 

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