Search This Blog

2009-10-29

Silverlight with LINQ and WCF

Microsoft Silverlight is a cross-browser, cross-platform, and cross-device plug-in for delivering the next generation of .NET based media experiences and rich interactive applications for the Web.Silverlight applications are delivered to a browser in a text-based markup language called XAML.
On the otherhand,LINQ is a programming model that introduces queries as a first-class concept into any Microsoft .NET language.
Additionaly,WCF is a technology by which pieces of software can communicate with one another.
These three technologies (Silverlight,LINQ,WCF) are the future of .NET.So you can't go further avoiding thess new technologies.I have tried to describe the interoperability between Silverlight,LINQ and WCF.
In the following example, data are coming from Sql server table with the help of LINQ,then the data are exposed by WCF for the client and atlast it cosumed by Silverlight application.

This can be achived by the following steps :

1.Create table in Sql Server database.
2.Create a silverlight application.
3.Linq To SQL to retrive the data from SQL server Database to .NET application.
4.Create WCF Service to receive the data for Silverlight application.
5.Consume data from WCF and display it in the datagrid of a silverligt application.

Create table in Sql Server database.

Step 1.Open SQL server Management Studio.
Step 2.Create a new database .

Step 3.write the database name "MyTestDb"

Step 4.create a new table "tblMyTest" as follows.

Step 5.Insert Some data in that table


Create a silverlight application

For this go to Start->Select Vs2008->Select Project In File Menu->select->SivlerLight. Then Select SilverLightAppliction in Templete


Put "SilverligtTest"

Create a Silver light Project :-


Choose "Asp.net web application project" under "new web project type"



Click OK

VS2008 create two project under solution:
SilverligtTest
SilverligtTest.Web

Linq To SQL to retrive the data from SQL server Database to .NET application.

LINQ is a very powerful addition to both VB 9 and C# 3 and is likely to be a central technique for data retrieval for Silverlight and other .NET technology going forward.

To begin right click on the SilverligtTest.Web project and choosing Add, and then choose the LinqToSql Classes template.Name it "MyDataClasses.dbml"



When the Object Relational Designer window opens, open the Server Explorer and navigate to the "MyTestDb" database and drag the tblMyTest Tables onto the MyDataClasses.dbml Designer workspace.
next click design surface of dbml (MyDataClasses.dbml) change the property of Serilaization mode from None to Unidirectional.



Create WCF Service to recevie the data for Silverlight application:

Right click on the SilverligtTest.Web project and choose Add New and from the templates choose WCF Service.Put "MyService.svc" in the name.



Open the file, IMyService.cs which contains the contract that was created by Visual Studio 2008.Write a method "GetAllEmployee"

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;


namespace SilverligtTest.Web
{
// NOTE: If you change the interface name "IMyService" here, you must also update the reference to "IMyService" in Web.config.
[ServiceContract]
public interface IMyService
{
[OperationContract]

List<tblMyTest> GetAllEmployee();

}
}


Having changed the contract in the interface, you must be sure to change the implementation in the .cs file.Open MyService.svc.cs.And implement the method GetAllEmployee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace SilverligtTest.Web
{
// NOTE: If you change the class name "MyService" here, you must also update the reference to "MyService" in Web.config.
public class MyService : IMyService
{


#region IMyService Members

public List<tblMyTest> GetAllEmployee()
{
//First create instance of DataClasses1
MyDataClassesDataContext db = new MyDataClassesDataContext();
var emp=db.tblMyTests;
return emp.ToList();

}

#endregion
}
}

Changes are required in web.config of SilverligtTest.Web

WCF uses wsHttpBinding as its default binding, now change it basicHttpBinding.
<services>
<service behaviorConfiguration="SilverligtTest.Web.MyServiceBehavior" name="SilverligtTest.Web.MyService">
<endpoint address="" binding="basicHttpBinding" contract="SilverligtTest.Web.IMyService">
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>

Build your SilverligtTest.Web application

Consume data from WCF and display it in the datagrid of a silverligt application.

Right click on SilverligtTest application and click on "Add Service Reference".

When the Add Service Reference,then click on Discover .The service you created will be found. Before clicking OK notice that by clicking on the Service, the operation you created is discovered.



Clicking OK button adds the service to your project. You will access the Web Service (and its method) through this reference.

Open MainPage.xaml of SilverligtTest and create a grid an button on this page.

<UserControl xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" x:Class="SilverligtTest.MainPage"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">
<Grid x:Name="LayoutRoot">
<data:DataGrid x:Name="myDataGrid" AlternatingRowBackground="Beige" AutoGenerateColumns="True" Width="700" Height="500" Grid.Row="2" Grid.Column="1" CanUserResizeColumns="True"></data:DataGrid>
<Button x:Name="btnSubmit" Content="Submit" Height="30" Width="50" Click="btnSubmit_Click" ></Button>
</Grid>
</UserControl>

Now call the service Asynchronously.

Go to MainPage.xaml.cs and write the following code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverligtTest
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
}

private void btnSubmit_Click(object sender, RoutedEventArgs e)
{
ServiceReference1.MyServiceClient obj = new SilverligtTest.ServiceReference1.MyServiceClient();
obj.GetAllEmployeeCompleted +=new EventHandler(obj_GetAllEmployeeCompleted);
obj.GetAllEmployeeAsync();

}

void obj_GetAllEmployeeCompleted(object sender, SilverligtTest.ServiceReference1.GetAllEmployeeCompletedEventArgs e)
{
myDataGrid.ItemsSource = e.Result;
}
}
}

Now its time to run your application .Press F5 to run.



Clicking on the button,you can see the output as follows



Now you can realize the interoperability of Silverlight ,LINQ and WCF.

No comments: