Srinivasnerella’s Weblog

May 9, 2008

Silver Light 2.0 with WCF and Linq in Vs2008

Filed under: SilverLight2.0 — srinivasnerella @ 11:30 am

What is Silver Light

 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 supports the display of high-definition video files, and importantly, Microsoft will do the heavy lifting of sending them over the Net. Silverlight applications are delivered to a browser in a text-based markup language called XAML.

In This Article i used Some New Topics these are

1) Linq-To Query and retrive  data for our silver light application

2)Connecting WCF webservice(Windows communication foundation)

3)Sivler light control like Datagrid .

Getting Started: First Create a Silver light Project :- for this go to  Start->Select Vs2008->Select Project In File Menu->select->SivlerLight.  Then Select SilverLightAppliction in Templete  
 

Create a Project named SilverlightSample, choose project type as web appliction project as  we want both a Silverlight project and also a Server project .

VS2008 create two project under solution:

The solution and first project are named SilverSlightApplcationData.  That first project is the Silverlight application and has the same files that are page.xaml,app.xaml etc.

The second project, SilverSlightApplcationData_Web is created for you as a test environment for the Silverlight project and it has three potential entry points,

  • Default.aspx
  • SQLDataTestPage.aspx
  • SQLDataTestPage.html 

Next Step: Adding Linq to Sql 

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.In this tutorial we’ll be writing

To begin right click on the server project and choosing Add, and then choose the LinqToSql Classes template.   

When the Object Relational Designer window opens, open the Server Explorer and navigate to the Northwind database  and  drag the Northwind Ttables onto the DataClasses1.dbml Designer workspace.

next click design surface of dbml (DataClass1.dbml) change the property of Serilaization mode from None to Unidirectional.

Adding Web Service:

Right click on the test project and choose Add New and from the templates choose WCF Service,

The result is the creation of three new files that hold the service contract for your WCF web service,

Open the first file, IService1.cs which contains the contract that was created by Visual Studio 2008.

for example Write a method 

pubulic  interface iservce1

{
[OperationContract]
list<Product>GetAllProduct();
}

Having changed the contract in the interface, you must be sure to change the implementation in the .cs file.
public list<Product>GetAllProduct()
{
//First create instance of DataClasses1
DataClaseesDatacontex db=new DataClassesDataContex();
 var product=db.Products
                      select p;

return product.Tolist(); 
}

 Next Web.Config:
Changes are required in web.config 

WCF uses wsHttpBinding as its default binding, now change it basicHttpBinding.
<Services>

<Service>
<endpoint address=”” binding=”basicHttpBinding” contract=”SQLData_Web.IService1“>
</Service>
</Services>

Next Add server reference  to silverlight application:

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 (GetAllProducts in this image showing GetcustoerByLastName) is discovered.

 

 

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

 Next Create XAML Page:
        

drag a DataGrid from the Toolbox onto the XAML.

<my:DataGrid x:Name=”theDataGrid” AlternatingRowBackground=”Beige” AutoGenerateColumns=”True” Width=”700″ Height=”500″ Grid.Row=”2″ Grid.Column=”1″ CanUserResizeColumns=”True”  />

<my:Button X:Name =”btnSubmit” Content=”Submit” Height=30 width=50 Click=btnSubmit_Click/>

 

Call the Service Asynchronously:

Steps:

 void  btnSubmit_Click( object sender, RoutedEverntArgs e)
{
Servicerefence1.Service1Client serive=new Servicerefence1.ServiceClient();

service.GetAllProductsCompleted+=(Press Tab two time it will add event handler)new EeventHandler<service.GetAllProductsCompleted>(service_GetAllproducts);
//Finally make asnychronous call
service.GetAllProductsAsync();
}

 next

void service_GetAllproducts(object sender,servicerefernce1.GetAllProductcompleted e)
{
theDataGrid.ItemSource=e.Result;

}

Now  run the project it show the out put .

 

 

 

Create a free website or blog at WordPress.com.