2012-01-13 76 views
0

我有一個啓用silverlight的WCF服務,其中一個方法絕對需要 [STAOperationBehavior]屬性。我需要爲用戶訪問用戶詳細信息(表單身份驗證),但在應用[STAOperationBehavior]屬性時,Membership.GetUser()失敗。使用[STAOperationBehavior]屬性獲取WCF服務中的用戶信息

[STAOperationBehavior] 
    [OperationContract] 
    public string DoWork(int inputStuff) 
    { 
    Membership.GetUser();//Fails 
    } 

//NOT ON STA THREAD 
    [OperationContract] 
    public string DoWork(int inputStuff) 
    { 
    Membership.GetUser();//Works 
    } 

我如何可以訪問該方法的用戶信息,或以其他方式提供該方法與用戶的信息?

回答

0

我終於解決了這個通過移除STAOperationBehavior屬性和手動上STA線程執行方法:

//NOT ON STA THREAD 
    [OperationContract] 
    public void DoWork(int inputStuff) 
    { 
     //Get the user info while we're not in an STA thread 
     var userDetails = Membership.GetUser(); 


     System.Threading.Thread thread = new System.Threading.Thread(new System.Threading.ThreadStart(delegate 
      { 
       //Do STA work in here, using the userDetails obtained earlier 
      })); 

     thread.SetApartmentState(System.Threading.ApartmentState.STA); 
     thread.Start(); 
     thread.Join(); 
    } 

有點亂,但我發現這樣做

別無他法