Informez-vous sur l'utilisateur dans WCF service avec l'attribut [STAOperationBehavior]
-
28-10-2019 - |
Question
J'ai un WCF service a permis de silverlight, dont une méthode exige absolument l'attribut [STAOperationBehavior]. Je dois détails de l'utilisateur d'accès (authentification par formulaire) pour l'utilisateur, mais Membership.GetUser () tombe en panne alors l'attribut est appliqué [STAOperationBehavior].
i.e..
[STAOperationBehavior]
[OperationContract]
public string DoWork(int inputStuff)
{
Membership.GetUser();//Fails
}
//NOT ON STA THREAD
[OperationContract]
public string DoWork(int inputStuff)
{
Membership.GetUser();//Works
}
Comment puis-je accéder à des informations utilisateur dans cette méthode, ou autrement fournir cette méthode avec les informations de l'utilisateur?
La solution
I finalement résolu ce problème en supprimant l'attribut STAOperationBehavior et l'exécution du procédé sur un fil de STA manuellement:
//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();
}
Un peu compliqué, mais je l'ai trouvé aucune autre façon de le faire
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow