Domanda

Ho un servizio WCF abilitato per Silverlight, di cui un metodo richiede assolutamente L'attributo [staoperationbehavior]. Devo accedere ai dettagli dell'utente (Autenticazione dei moduli) per l'utente, ma l'appartenenza.getUser () non riesce mentre viene applicato l'attributo [StaoperationBehavior].

cioè

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

ma

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

Come posso accedere alle informazioni dell'utente in questo metodo o fornire altrimenti questo metodo le informazioni dell'utente?

È stato utile?

Soluzione

Alla fine ho risolto questo rimuovendo manualmente l'attributo staperationbehavior ed eseguendo il metodo su un thread 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();
    }

Un po 'disordinato ma non ho trovato altro modo di farlo

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top