Window Support

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Tuesday, 20 January 2009

Silverlight 2 : Maintain data in Session using Silverlight-enabled WCF service.

Posted on 06:30 by Unknown

A piece of paper was thrown in front of me on table by my friend in local coffee shop, that paper was nothing but print out of one already designed ASP.NET site which now he want to convert to Silverlight with few of pages as kind of POC for Silverlight. I just saw “Welcome : <Username>” label on that, and I asked him how he is taking care of Sessions in Silverlight? and a puzzle for him..since he never implemented, there are several solutions on net by many people and enthus, here is my piece of code which I feel simple, offcourse you can try that out and help me improve if there any better way to do so.

To show this particular functionality, I am re-using my old code of Master pages in Silverlight since I need more than one page with navigation which you can find it here :

http://pendsevikram.blogspot.com/2008/11/silverlight-2-master-pages.html

Also, if you are not comfortable with Silverlight-enabled WCF services , you can see my old article :

http://pendsevikram.blogspot.com/2008/10/silverlight-2-grid-with-silverlight.html

Step 1 : Create 2 Silverlight Pages

Page.xaml :

<Grid x:Name="LayoutRoot" Background="AliceBlue">
        <TextBlock Height="23" Margin="94,8,116,0" VerticalAlignment="Top" Text="Explore .NET with Vikram Pendse" TextWrapping="Wrap" Foreground="#FF000000"/>
        <StackPanel x:Name="stk" Width="400" Margin="0,66,0,30">
            <TextBlock Margin="78,10,0,30" Text="UserName:"/>
            <TextBox Margin="40,-50,0,30" x:Name="txtuser" Height="20" Width="150" />

<TextBlock Margin="78,-5,25,30">Password :</TextBlock>
            <PasswordBox Margin="40,-60,0,10" x:Name="txtpassword" Height="20" Width="150" />
            <Button x:Name="btnSubmit" Height="20" Width="100" Content="Login" Click="btnSubmit_Click" />
        </StackPanel>
        <TextBlock Height="23" Margin="106,0,104,3" VerticalAlignment="Bottom" Text="Microsoft .NET for everyone!!" TextWrapping="Wrap" Foreground="#FF000000"/>
</Grid>

Welcome.xaml :

<Grid x:Name="LayoutRoot" Background="AliceBlue">
        <StackPanel x:Name="mystk" Width="400" Margin="0,66,0,30">
            <TextBlock Margin="128,-5,0,30" Text="Welcome :"/>
            <TextBlock Margin="196,-46,0,30" x:Name="txtusername" />
        </StackPanel>     
</Grid>

Step 2 : Add a Silverlight-enabled WCF Service and access it in Silverlight application

MyService.svc.cs :

using System.Web;

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class MyService
    {
        HttpContext myctxt = HttpContext.Current;

        [OperationContract]
        public string Login(string username, string password)
        {
            //Validate Username and Password against the Database.
            myctxt.Session["Uname"] = username;
            return username;
        }

        [OperationContract]
        public string ReturnUser()
        {
            string user;
            user = (string)myctxt.Session["Uname"];
            return user;
        }

Note : For demo purpose, I have not validated User-id and Password against database, please refer comment.

Step 3 : Consume SL-enabled WCF service in each page and pass the corresponding parameters.

First we need to send username and password from Page.xaml.cs to Service, Then Login() will maintain that particular username in session, Then we will again call the service and access that particular username which is in session by ReturnUser()

Page.xaml.cs :

private void NavigateRequest(int w)
{
     if (w > 0)
     {
         stk.Children.Clear(); 
         stk.Children.Add(new Welcome());
      }
      else
      {
          this.Content = new Page();
      }            
}

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
    Srvc.MyServiceClient proxy = new SL_SessionMgmt.Srvc.MyServiceClient();
    proxy.LoginAsync(txtuser.Text,txtpassword.Password);
    NavigateRequest(1);
}                

Here we are switching among the pages by NavigateRequest(), you can write Switch-case if there are multiple pages.

Welcome.xaml.cs :

public Welcome()
{
    InitializeComponent();
    this.Loaded += new RoutedEventHandler(Welcome_Loaded);
}

void Welcome_Loaded(object sender, RoutedEventArgs e)
{
     Srvc.MyServiceClient proxy = new SL_SessionMgmt.Srvc.MyServiceClient();
     proxy.ReturnUserCompleted += new EventHandler<SL_SessionMgmt.Srvc.ReturnUserCompletedEventArgs>(proxy_ReturnUserCompleted);
     proxy.ReturnUserAsync();
}

void proxy_ReturnUserCompleted(object sender, SL_SessionMgmt.Srvc.ReturnUserCompletedEventArgs e)
{
      txtusername.Text = e.Result;
}

And here we are getting username which is in session by e.Result, This will display the username in session on Textblock on second page, it will look like this.

SLSession1

Here you can see whatever we pass as username, we place it in session by WCF service and get that data from session via service again and bind it to another control on another page.

SLSession2

Hope this will useful for you, if you are planning to implement Sessions in your Silverlight applications.

Vikram. 

Email ThisBlogThis!Share to XShare to FacebookShare to Pinterest
Posted in | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • First Windows Phone 7 update February 2011 - Small update but Big start
    After tons of rumors and set of predictions on Windows Phone 7 all over Internet, Microsoft came up with first Windows Phone 7 minor update ...
  • The little Story of “I Unlock Joy” event by Microsoft and Pune User Group
      This post is about recent “I Unlock Joy” event happened in Pune which was conducted by Microsoft and Pune User Group. Little History : ...
  • Silverlight On Mobile : Windows Phone 7 Splash Screen and Customization
    After talking about 3D capabilities on Windows Phone 7 using Silverlight in last article , Now I am moving ahead with small but equally impo...
  • Silverlight 5 : Platform Invoke (PInvoke) in Silverlight
      Two days back Microsoft announced availability of Silverlight 5 RC,I encourage you to download bits from here , My friend Pete Brown alr...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 1
    After a long..I am writing blog, I hope and I wish I will resume blogging like I use to in past. Lots of things happened in past few months....
  • MCTS : Microsoft Silverlight 4 Development Exam Guide (70-506) by Packt Publishing
      Hello, After a long time I got chance to come back here.I will soon resume blogging in month of August. Last 4-5 months were horrible due...
  • Introduction to Speech Capabilities in Windows Phone 8 – Part 2
    Hope you enjoyed my last article on Speech Capability in Windows Phone 8, Today I am posting another part or you can say little extension t...
  • Silverlight 3 : Insert & Update Data using WCF Service with DataForm and DataGrid
    In my Lap around Silverlight 3 series, I have written a separate article on DataForm in Silverlight 3, This article is a basic extension to ...
  • Mango : Using DeviceStatus in Windows Phone 7.1
    First of all “Thank You” for your wonderful response and comments on my last article on Silverlight Vs HTML5 ,I hope you like the points I ...
  • Silverlight, HTML5 & Windows 8 : Where we are heading to ?
    This is not the post or yet another post on most happening debate of Silverlight and HTML5, This is just a visit to all of them to realize t...

Blog Archive

  • ►  2013 (4)
    • ►  August (1)
    • ►  April (3)
  • ►  2012 (4)
    • ►  July (1)
    • ►  March (2)
    • ►  January (1)
  • ►  2011 (24)
    • ►  December (1)
    • ►  September (4)
    • ►  August (2)
    • ►  July (1)
    • ►  June (4)
    • ►  May (3)
    • ►  April (3)
    • ►  March (1)
    • ►  February (4)
    • ►  January (1)
  • ►  2010 (21)
    • ►  December (1)
    • ►  November (2)
    • ►  October (3)
    • ►  September (2)
    • ►  August (4)
    • ►  July (5)
    • ►  May (1)
    • ►  April (1)
    • ►  March (1)
    • ►  January (1)
  • ▼  2009 (49)
    • ►  December (1)
    • ►  November (5)
    • ►  October (2)
    • ►  September (1)
    • ►  August (5)
    • ►  July (5)
    • ►  June (1)
    • ►  May (5)
    • ►  April (5)
    • ►  March (9)
    • ►  February (4)
    • ▼  January (6)
      • Microsoft Certification - Discount Code for MCTS,M...
      • Lap Around Microsoft Web Platform Installer 1.0 ak...
      • Silverlight 2 : Maintain data in Session using Sil...
      • Microsoft Tag : New way to go Online !!
      • Lap around Report Builder 2.0
      • Silverlight 2 : Using Magnifier to Zoom contents o...
  • ►  2008 (43)
    • ►  December (3)
    • ►  November (9)
    • ►  October (7)
    • ►  September (4)
    • ►  August (2)
    • ►  July (3)
    • ►  June (4)
    • ►  May (3)
    • ►  March (3)
    • ►  February (5)
Powered by Blogger.

About Me

Unknown
View my complete profile