2011-02-23 5 views
0

클라이언트 쪽에서 비동기 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()에서 액세스 할 수 있습니다.

아무도 왜 내 세션 상태가 없어지는지 알 수 있습니까?

  1. asyn 호출이 스레드로부터 안전하지 않기 때문에 스레드가 세션 컨텍스트를 잃어 버릴 수 있습니까?
  2. 여기서 세션에 액세스 할 수있는 방법이 있습니까?

답변

0

경우 누군가가 찾을 수도 있습니다 유용

BeginXXX 새로운 theard을 만들 수행하고 스레드

세션 + 비동기은 =

실패, 명백한 이유를 일반적으로
0

에 대한 세션 안전하지 않습니다

콜백 내에서 안전하고 비동기 코드로 분기하기 전에 안전하지만 병렬 상태에서 전체 세션 상태를 얻지는 않습니다.

그러나 몇 가지 해결 방법이 있습니다. 가장 쉬운 방법은 세션 값에 대한 읽기 전용 액세스가 허용되는 컨트롤러 수준에서

입니다. 당신이 시도하지 않는 한 병렬로 작동 할 것입니다 세션에 씁니다. 당신은 또한 자신의 HttpHandler를 구현할 수 있지만, 조금 힘들 수 있습니다.