nastoski
Technical User
- Oct 21, 2003
- 21
Dear all,
I’m trying to figure out how session variables work in web services, but obviously I found myself in trouble. I would appreciate your help.
I create a web service with following Global.asax and Service.asmx files
//Global.asax
…
protected void Session_Start(Object sender, EventArgs e)
{
Session[“TID”] = 1;
}
//Service.asmx
…
[WebMethod(EnableSession=true)]
public int CalculateSession()
{
Session[“TID”] = ((int)Session[“TID”]) + 1;
return (int) Session[“TID”];
}
…
Problem starts when I call a web service from Console Application (previously I add web service in a project via Add Web references)
namespace Client
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
localhost.WebService ws = new localhost.WebService()
for (int i=0; i<10;i++)
{
Console.WriteLine (“session {0}”,ws.CalculateSession);
}
}
}
}
When running client program I always get one same response
session 2
session 2
session 2
session 2
session 2
…
This is kind of strange to me, because I expected that with localhost.WebService ws = new localhost.WebService() I would open a session and Session[“TID”] that I would get will be incremented by 1, but obviously I’m wrong. Can someone explain what am I doing wrong.It seems that every time i call a method from webservice new session is opened and the new instance of class is created.
Thanks in advance,
Regards,
Igor Nastoski
I’m trying to figure out how session variables work in web services, but obviously I found myself in trouble. I would appreciate your help.
I create a web service with following Global.asax and Service.asmx files
//Global.asax
…
protected void Session_Start(Object sender, EventArgs e)
{
Session[“TID”] = 1;
}
//Service.asmx
…
[WebMethod(EnableSession=true)]
public int CalculateSession()
{
Session[“TID”] = ((int)Session[“TID”]) + 1;
return (int) Session[“TID”];
}
…
Problem starts when I call a web service from Console Application (previously I add web service in a project via Add Web references)
namespace Client
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
localhost.WebService ws = new localhost.WebService()
for (int i=0; i<10;i++)
{
Console.WriteLine (“session {0}”,ws.CalculateSession);
}
}
}
}
When running client program I always get one same response
session 2
session 2
session 2
session 2
session 2
…
This is kind of strange to me, because I expected that with localhost.WebService ws = new localhost.WebService() I would open a session and Session[“TID”] that I would get will be incremented by 1, but obviously I’m wrong. Can someone explain what am I doing wrong.It seems that every time i call a method from webservice new session is opened and the new instance of class is created.
Thanks in advance,
Regards,
Igor Nastoski