I have a problem - if I access the session in Web Service, as soon as I
store
anything in it, requests are no longer running in parallel - instead,
they are
queued and executed sequentially. Here is an example (ASP.NET 2.0, VS
2005).
Web service method (web service is a regular asmx):
[WebMethod(EnableSession = true)]
public String DoNothing()
{
Thread.Sleep(10000);
// HttpContext.Current.Session["initvar"] = 123;
return HttpContext.Current.Session.SessionID;
}
Client (console application with web reference):
class Program
{
static void Main(string[] args)
{
Program pr1 = new Program();
Program pr2 = new Program();
System.Net.CookieContainer cc = new
System.Net.CookieContainer();
localhost.CellSearchService svc1 = new
localhost.CellSearchService();
svc1.CookieContainer = cc;
Console.WriteLine(svc1.DoNothing());
localhost.CellSearchService svc2 = new
localhost.CellSearchService();
svc2.CookieContainer = cc;
svc1.DoNothingCompleted += new
WebSvcTester.localhost.DoNothingCompletedEventHand ler(pr1.svc_DoNothingCompleted);
svc2.DoNothingCompleted += new
WebSvcTester.localhost.DoNothingCompletedEventHand ler(pr2.svc_DoNothingCompleted);
svc1.DoNothingAsync();
svc2.DoNothingAsync();
Console.In.ReadLine();
}
void svc_DoNothingCompleted(object sender,
WebSvcTester.localhost.DoNothingCompletedEventArgs e)
{
Console.WriteLine(e.Result);
}
}
See that first call is sync, and next 2 are async. If in web service
method I have this line commented out:
// HttpContext.Current.Session["initvar"] = 123;
then async calls are processed in parallel, and for all 3 calls I get
different session ids. But if I uncomment it, async calls are executed
sequentially, and all 3 session ids are the same.
Looks like as soon as something is stored in a session (thus, session
is actually used), something in .NET framework prevents requests from
multithreading. Can I make it not so intelligent and leave this
decision to me? I will take care of threads synchronization myself.