我正在調用客戶端的異步調用PageMethod
。後端代碼是BeginInvoke方法和會話狀態
[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
}
會話狀態是從BeginMethodCall()
和EndMethodCall()
但不能從MethodCall()
訪問。
有誰能告訴我爲什麼我在這裏失去我的會話狀態?
- 線程是否會丟失會話上下文,因爲asyn調用不是線程安全的?
- 有沒有辦法在此訪問會話?