Using the WebMethod property EnableSession to save data
Here is a small, simplified sample of my code:
[System.Web.Services.WebMethod(EnableSession=true)]
public void StartGame()
{
numTurns = 8;
}
[System.Web.Services.WebMethod(EnableSession=true)]
public int getTurns()
{
return numTurns;
}
When I debug this web service (VS 2008) I invoke StartGame first, then I close out of that tab and invoke getTurns. It should print out 8, but it prints out 0. What could the problem be?
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

naura paxRank #2,843 · 502 reputationPosted Nov 1, 2009, 8:23 PM EST
You need to check exactly where have you declared your webservice object, may be at the time of closing tab it loses its value and is reinitialized.
You would need to persist the value of WebService object, use session or viewstate for that.
here is an example :
// C# public class Service1 : System.Web.Services.WebService { [System.Web.Services.WebMethod(EnableSession=true)] public double ConvertTemperature(double dFahrenheit) { Session["Conversions"] = (int) Session["Conversions"] + 1; return ((dFahrenheit - 32) * 5) / 9; } [System.Web.Services.WebMethod(EnableSession=true)] public int GetNumberOfConversions() { return (int) Session["Conversions"]; } }