Get User info in WCF-service with [STAOperationBehavior] attribute

528 views Asked by At

I have a silverlight-enabled WCF-service, of which one method absolutely requires the [STAOperationBehavior] attribute. I need to access user details (Forms Authentication) for the user, but Membership.GetUser() fails while the [STAOperationBehavior] attribute is applied.

i.e.

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

but

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

How can I access user information in this method, or otherwise provide this method with the user's information?

1

There are 1 answers

0
user495625 On BEST ANSWER

I eventually solved this by removing the STAOperationBehavior attribute and executing the method on an STA thread manually:

    //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();
    }

A bit messy but I found no other way of doing it