Sharing ASP Session state with Silverlight

I’ve been working on a Silverlight project that where the SL application is used within an authenticated ASP.net site. During my testing I found a problem where using Silverlight 3’s Out-Of-Browser feature seem to lose the session token between calls to the site, i.e. I could logon but the next call would fail as if the user had not logged on. After a couple of Twitter conversations it became apparent that it should work. So I decided to take my testing back to basics, so first off can a Silverlight application use ASP session state?

  1. Create a basic Silverlight application – create a Silverlight application with a web project to host it
  2. Create a basic web service – with the web project selected add a new Silverlight WCF service, called SessionTest.svc
    image
  3. Implement some very simple interactions with the Session State

    [ServiceContract(Namespace = "")]

        [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]

        public class SessionTest

        {

            [OperationContract]

            public void SetState(string value)

            {

                // Add your operation implementation here

                HttpContext.Current.Session["TestState"] = value;

                return;

            }

     

            [OperationContract]

            public string GetState()

            {

                // Add your operation implementation here

                return (string)HttpContext.Current.Session["TestState"];

            }

        }

  4. Add a Service Reference from your Silverlight application to your service, add appropriate using statements to your code-behind.

  5. Create some simple controls in your Silverlight application

    <StackPanel>

        <StackPanel Orientation="Horizontal">

            <Button x:Name="ButtonGetState"  Content="Get State" Click="ButtonGetState_Click" Width="60" ></Button>

            <TextBlock x:Name="TextBlockCurrentState" Width="100"></TextBlock>               

        </StackPanel>

        <StackPanel Orientation="Horizontal">

            <TextBox x:Name="TextBoxNewState" Text="" Width="100"></TextBox>

            <Button x:Name="ButtonSetState" Content="Set State" Click="ButtonSetState_Click" Width="60"></Button>

        </StackPanel>

    </StackPanel>

  6. Add code to the Silverlight application to communicate with the Service

    private void ButtonGetState_Click(object sender, RoutedEventArgs e)

            {

                SessionTestClient client = new SessionTestClient();

                client.GetStateCompleted += (object completedSender, GetStateCompletedEventArgs completedE) =>

                    {

                        if (completedE.Error == null)

                        {

                            TextBlockCurrentState.Text = completedE.Result;

                        }

                    };

                client.GetStateAsync();

            }

     

            private void ButtonSetState_Click(object sender, RoutedEventArgs e)

            {

                SessionTestClient client = new SessionTestClient();

                client.SetStateCompleted += (object completedSender, AsyncCompletedEventArgs completedE) =>

                    {

                        if (completedE.Error == null)

                        {

                            TextBlockCurrentState.Text = "";

                        }

                    };

                client.SetStateAsync(TextBoxNewState.Text);

            }

  7. Run the code, you should see that after you set a value you can retrieve the same value.

So we now know for sure that we can communicate with ASP.net and share the session state with the page. If you make the Silverlight application OOB (select project properties and check the OOB box) you’ll see that it continues to work without any problems. So this is good news, all I have to do now is track down why it isn’t working in my real application.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Sharing ASP Session state with Silverlight

  1. pauliom says:

    I should add the problem with ‘my real site’ was one of URIs. Setting was using localhost, getting was using full domain name – thus the cookies were different

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s