我有一个支持Silverlight的WCF服务,其中一个方法绝对需要 [STAOperationBehavior]属性。我需要为用户访问用户详细信息(表单身份验证),但在应用[STAOperationBehavior]属性时,Membership.GetUser()失败。
即
[STAOperationBehavior]
[OperationContract]
public string DoWork(int inputStuff)
{
Membership.GetUser();//Fails
}
但
//NOT ON STA THREAD
[OperationContract]
public string DoWork(int inputStuff)
{
Membership.GetUser();//Works
}
如何在此方法中访问用户信息,或以其他方式为此方法提供用户信息?
答案 0 :(得分: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();
}
有点乱,但我没有找到其他办法