Silverlight

Silverlight controls are client side controls. They get executed on the browser on the client machines and they do not have direct access to the server side data.Since Session variables live on the web server, Silverlight controls cannot access them directly. However, there are several ways Silverlight can retrieve the server side data including Session information.Let us explore some of the possible options to make Session data available to Silverlight controls.

Silverlight is a cross platform, cross browser .NET plug-in that enables designers and developers to build rich media experiences and RIAs for browsers.Microsoft Silverlight is a programmable web browser plugin that enables features such as animation, vector graphics and audio-video playback that characterise rich Internet applications. Version 2.0, released October 2008, brings additional interactivity features and support for .NET languages and development tools. It is compatible with multiple web browser products used on Microsoft Windows and Mac OS X operating systems. Mobile devices, starting with Windows Mobile 6 and Symbian (Series 60) phones, will also be supported.

Silverlight provides a retained mode graphics system similar to Windows Presentation Foundation, and integrates multimedia, graphics, animations and interactivity into a single runtime environment. It is being designed to work in conjunction with XAML and is scriptable with JavaScript. XAML can be used for marking up the vector graphics and animations. Textual content created with Silverlight is searchable and indexable by search engines as it is not compiled, but represented as text (XAML). Silverlight can also be used to create Windows Sidebar gadgets for Windows Vista.

Silverlight ships with a lightweight class library which includes features such as extensible controls, XML Web Services, networking components and LINQ APIs. This class library is a subset of, and is considerably smaller than, .NET Framework's Base Class Library. Silverlight code runs in a sandbox, thus preventing the invocation of platform APIs.

There are some more important features of Silverlight:

  • Incorporates Windows Presentation Foundation Core
  • Contains CLR (Common Language Runtime) and most important base classes from the .NET Framework
  • Supports programming in C#, VB, JScript, Ruby, Python
  • Embeds Communication Framework including WCF (Windows Communication Framework)
  • Comes with Data Framework including LINQ

Silverlight applications can be written in any .NET programming language. As such, any development tools which can be used with .NET languages can work with Silverlight, provided they can target the Silverlight CoreCLR for hosting the application, instead of the .NET Framework CLR. Visual Studio 2008 can be used to develop and debug Silverlight applications. To create Silverlight projects and let the compiler target CoreCLR, Visual Studio 2008 requires the Silverlight Tools for Visual Studio.

  • Push session data to the client in the form of VIEWSTATE data or data for UI controls. The dis advantage with this approach are no type safety, only minimal data can be sent, data is visible to users.
  • Use the InitParameters property of the Silverlight controls to push data to the Silverlight controls. This approach also have problems like no type safety, only minimal data can be sent.
  • Use WCF service calls to retrieve data from server. Using this approach, Silverlight controls can call WCF services to retrieve larger volume of data without having the disadvantages of the above 2 options.

To use WCF to get Session data from server to Silverlight controls, all you need to do is create a simple WCF service. This service should have a method that takes the session key as input parameter and return the corresponding value from the session, as shown below:

  [OperationContract]
  public object GetSessionVariable(string key)
    {    
      return System.Web.HttpContext.Current.Session[key];
    }

.xap file is the compressed output file for the Silverlight application. The .xap file includes AppManifest.xaml, compiled output assembly of the Silverlight project (.dll) and any other resource files referred by the Silverlight application.

Web pages like .aspx files and .html files use the Silverlight components by loading the .xap files using the <object> tag in the HTML or by using <asp:Silverlight> tag in the ASP.NET pages.

".xap" files (pronounced "zap") use the standard .zip compression algorithm to minimize client download size. A "hello world" .NET Silverlight application (built using VB or C#) is about 5KB in size.

Let us see how a .xap file look like.

Open Visual Studio and create a new project by selecting "Silverlight Application" under the project type "Silverlight". Choose the project name "SilverlightTest". In the next screen, choose the option "automatically generate a test page to host Silverlight at build time".

Visual Studio creates a project with 2 files:

1. App.xaml
2. Page.xaml

Compile and run the application by pressing Ctrl + F5. You can see the browser opening with an empty page (the page is empty because you have nothing in your default xaml file).

Now open your windows explorer and look in to the bin\debug folder of your project. You can see a file called "SilverlightTest.xap".

Rename this file to SilverlightTest.zip and open it using any decompression tool. You can see that this is just like any other zip file and it includes the projects output dll and another file called "AppManifest.xaml".

blogspot stats