Reading Data from WCF Data Service in Silverlight 4
Posted by: Mahesh Sabnis
in Category Windows Communication Foundation (WCF)
Abstract: WCF data Services provides integration with Data Model built using ADO.NET EF. Since the data received is in the form of XML, any client application built on Open standards can easily consume it. In this article, I will demonstrate how it is done using Silverlight 4.
As part of .NET Framework 4.0 and Windows Communication Foundation (WCF) 4.0, ADO.NET Entity Framework integration is provided using WCF Data Services. The WCF Data Services exposes entity data (provided by ADO.NET EF) as a data service.
WCF Data Services enables the creation and consumption of OData services for the web. Note that WCF Data Service was formerly known as ADO.NET Data Services.
To design a WCF Data Service, we need to do the following:
Design the Data Model using ADO.NET EF:
This is done with the help of ADO.NET Entity Framework. This is based upon Object Relational Mapping (O-R-M) where we get the CLR objects mapped with the database table.
In this case, the query can be fired directly on the mapped objects which returns an instance of IQueryable interface.
Using this mapping, it is possible for us to perform Create, Read, Update and Delete operations on the Database.
Create a Data Service:
Configure Access Rules for the Data Service:
This is the most important step. Here you need to configure the Data Service which defines AccessRules for entities defined in the Data Model.
Based upon the operation you want to perform, the access rule must be specified.
DataServiceConfiguration class is used for Data Service configuration and setting Access Rules.
In this article demonstration, I have used Sql Server 2008 and the Database is ‘Company’ with the following table:
Step 1: Open VS2010 and create a Blank solution, name it as ‘WCF_DataService_And_Silverlight_4’
Step 2: In this solution, add a new WCF Service application and call it ‘WCF_DataService’. Delete IService1.cs and Service1.svc from the project, as we are going to make a use of the WCF Data Service Template.
Step 3: Right-click the project and add a new ADO.NET Entity Data Model, name it as ‘CompanyEDMX’. Follow the wizard, select ‘Company’ as database and select Department table. The EDMX file should get added in the project as shown below:
Step 4: Right-click on the project, name it as ‘WCF_DataService_Company.svc’ and add the new WCF Data Service in the project. Make the following changes in it:
Step 5: Build the project and make sure that it is error free.
Step 6: Right-Click on WCF_DataService_Company.svc and view in browser, you should see the following:
The Url will be ‘http://localhost:<some Port>/WCF_DataService_Company.svc/’
Step 7: Now change the Url to the one shown below:
you will find all rows from Department table in the form of an Xml document as below:
If for some reason, you do not see xml and instead you are getting a Feed, then make the following changes from Internet Options (in IE):
Step 7: In the same solution, add a new Silverlight application and name it as ‘SL4_Client_WCF_Data-Service’. Make sure that you will host it in the same WCF Service project created above by keeping the defaults shown in the following window:
This will add ‘SL4_Client_WCF_Data-ServiceTestPage.aspx’ and ‘SL4_Client_WCF_Data-ServiceTestPage.html’ in the ‘WCF_DataService’ project.
Step 8: In the Silverlight project, add the WCF service reference and name the service namespace as MyRef.
Step 9: Open MainPage.xaml and drag drop a button and DataGrid element in it:
Step 10: In the MainPage.Xaml.cs use the following namespaces:
- using System.Data.Services.Client;
- using System.Windows.Data;
- using SL4_Client_WCF_Data_Service.MyRef;
Step 11: In the MainPage class level, declare the following objects:
- MyRef.CompanyEntities objContext;
- DataServiceCollection<Department> deptDataCollection;
In .NET 4.0, the DataServiceCollection<T> class is introduced in WCF Data Services to support databinding with the controls in a client application. This class is especially used in WPF and Silverlight.
Step 12: In the UserControl_Loaded event, write the following code for creating an instance of the objectContext proxy for the WCF data service using the Service uri and an instance of DataServiceCollection:
Step 13: In the ‘Get Data’ button click event, make an async call to the WCF Data Service by firing the query as shown below:
The query is executed asynchronously and the result is generated which is further assigned to the ItemsSource property of the DataGrid.
Step 14: Run the application and click on Get Data button. The following output will be displayed:
Conclusion: WCF data Services provides integration with Data Model built using ADO.NET EF. Since the data received is in the form of XML, any client application built on Open standards can easily consume it.
The entire source code of this article can be download over here
Note: Here the aspx/html text page is in the same WCF service project, the WCF Service is currently hosted on the local port and not on IIS.
C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.
We at DotNetCurry are very excited to announce the pre-order of The Absolutely Awesome Book on C# and .NET. This is a concise technical eBook and will be available in PDF, ePub, and mobi.
Organized around concepts, this eBook aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core. Use these concepts in your next .NET Project or to crack your next .NET Interview.
Click here to Pre-Order this eBook at a Discounted Price!