2012-01-13 61 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(); 
    } 

有点乱,但我发现这样做

别无他法