Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Web service session problem

Status
Not open for further replies.

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 found some solution which didn't work for me :(

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()

CookieContainer ck = new System.Net.CookieContainer();

ws.CookieContainer = ck;
for (int i=0; i<10;i++)
{
Console.WriteLine (“session {0}”,ws.CalculateSession);
}
}
}
}

Regards,
Igor
 
I believe this is always returning the same result because the session is specific to the request. each request creates a new session. instead you may want to try using Application or Cache variables instead of Session. These are accessible across all requests.

instead of calling Session["TID"] call Application["TID"] or Cache["TID"].

remember that webservices, just like web forms, are stateless. By design they shouldn't remember what just happened, only what's happening right now.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top