I am invoking a asynchronous PageMethod call from the client-side. The backend code is
[WebMethod(EnableSession = true)]
public static string BeginMethodCall()
{ //Session Accessible here
string g = Guid.NewGuid().ToString();
Func<object> f = () => MethodCall();
IAsyncResult asyncCall = f.BeginInvoke(null, f);
lock (AsyncThreadPool)
AsyncThreadPool[g] = asyncCall;
return g;
}
[WebMethod(EnableSession=true)]
public static object EndMethodCall(string guId)
{
IAsyncResult callResult;
lock (AsyncThreadPool)
{
callResult = AsyncThreadPool[guId];
AsyncThreadPool.Remove(guId);
}
Func<object> f = (Func<object>)callResult.AsyncState;
callResult.AsyncWaitHandle.WaitOne();
return f.EndInvoke(callResult);
}
[WebMethod(EnableSession = true)]
public static object MethodCall()
{
//Session not accessible here
}
Session state is accessible from BeginMethodCall() and EndMethodCall() but not from the MethodCall().
Can anyone tell me why I lose my session state here?
- Do threads lose the Session context because asyn calls are not thread safe?
- Is there a way to access session here?
In case someone may find this useful
BeginXXX does create a new theard and threads are not session safe for obvious reasons