Tuesday, April 05, 2011

Efficient Paging In Silverlight 2.0

When it comes to ASP.NET, I am a huge fan of efficient code, and one of the most efficient ways to retrieve data from the server is to use paging. Paging has been around for a long time now, but it is not available out of the box in Silverlight 2.0. I thought this was a good time to demonstrate how to consume a WCF service in Silverlight, and to create efficient server side paging using LINQ.

Before we begin you need to have the Silverlight 2 Tools installed. You can go here to download it. 
To begin with open Visual Studio 2008 and choose File > New > Project > Silverlight >
Silverlight Application:

When you click OK a new dialog will appear. Accept the default settings. The rules of this application are to create a WCF service that returns records from the Windows event log. Some computers store event log entries that can contain hundreds of entries, so it is important to page through this data efficiently.   Add a new class to the web application and name it EventEntry. Add the following code to the class:

C#
public class EventEntry
{
public string Message { get; set; }      
}
public class TotalInfo
{
public List<EventEntry> Entries { get; set; }
      public int Total { get; set; }
}

The code above is self explanatory, but we will use it later. Next we need to create a Silverlight enabled WCF service. Right click the project and choose Add > New Item > Silverlight > Silverlight-enabled WCF service:

It is important to create a Silverlight enabled WCF service because the only binding supported by Silverlight 2.0 is basicHttpBinding. Once the WCF service is created, add the following code:

C#
[OperationContract]
public TotalInfo FetchEventLogEntries(int skip, int take)
{
      TotalInfo info = new TotalInfo();
      List<EventEntry> entries = new List<EventEntry>();
      using (EventLog log = new EventLog("Application"))
      {
            info.Total = log.Entries.Count;
            var query = log.Entries.Cast<EventLogEntry>()
                  .OrderByDescending(o => o.TimeWritten)
                  .Skip(skip).Take(take);
            foreach (var item in query)
            {
                  entries.Add(new EventEntry()
                  {
                        Message = item.Message
                  });
            }
            info.Entries = entries;               
      }
      return info;
}    

In the code above one method is decorated with the OperationContract attribute. This means that it is a public method that can be consumed by the Silverlight application. The method takes two arguments, skip and take. We use these two values in the LINQ query to page through the event log. The code uses the EventLog class which is in System.Diagnostics namespace. This gives you access to the Windows event log. I am using LINQ to query the event log and return an ordered list of event log entries and limit the amount of entries that are returned by the Skip and Take methods.

That’s the server code taken care of. Next is to build a Silverlight XAML file to display the data. Open the Silverlight project and view the Page.xaml file. Add the following XAML to the file:

<Grid x:Name="LayoutRoot" Background="Azure" HorizontalAlignment="Left" Width="Auto">
        <StackPanel Width="800" Height="800">
            <ListBox x:Name="lstEvent" Width="750" Height="500" Margin="10" BorderThickness="2" BorderBrush="Black">
                <ListBox.ItemTemplate>
                    <DataTemplate>                       
                        <Border BorderBrush="BurlyWood" BorderThickness="1" CornerRadius="4">
                            <TextBlock Text="{Binding Message}" x:Name="txtMessage" Width="710" TextWrapping="Wrap" />   
                        </Border>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

Read more: dot Net Curry