Consuming ADO.NET Data Service (Astoria) from Silverlight

This post describes how to access the ADO.NET Data Service (Astoria) from Silverlight by using “ADO.NET Data Services Silverlight Add-On“.

Download : SourceCode

ADP.NET Data Service (Astoria) from Silverlight (Silverlight ListView)

Contents

  • Introduction
  • Prerequisites
  • Getting Started
  • SQL Database
  • Creating Web Application in Visual Studio 20008
  • Creating ADO.NET Entity Data Model
  • Creating ADO.NET Data Service
  • Silverlight ListView
  • Conclusion

Introduction

This article is just an introduction of how to use Astoria in Silverlight application. The sample that I used in this article is nearly as same as the sample which is mentioned in ASP.NET 3.5 Extensions Preview (Quick Start). But I used the Astoria Silverlight Addon and create the Silverlight listview that looks cool.

Background

Project Astoria Team released ADO.NET Data Service library for Silverlight on 11st January, 2008. This is very interesting news for Silverlight developer. I was thinking to give a try when I heard that news but a lot of things keep me busy until 15th of Jan. Now, I got a chance to try and make this sample for you all. I hope you guys like it.

Prerequisites

You need to install the following installers in order to use Astoria Service, Silverlight.

Getting Started

After installing all required installers, you started creating new project to give a try how to use ADO.NET Data Service in Silverlight application. Like my other articles, I will give you the step-by-step details on how to consume Astoria in Silverlight. If you are not clear something, please just drop a comment in this post. I will get back to you as soon as I can.

SQL Database

You need to create one database that can be used in our sample. (If you already download the sample, you can find the SQL database called EmployeeMgmt.mdf in zip file.) If you want to create your own database, open SQL 2005 management studio and create new database. Then, create new table and fill some data in that table. (As this article is not for creating the database in SQL, I won’t go step by step. If you have some problems in doing this, please let me know.)

Creating Web Application in Visual Studio 20008

After creating the database, we will start creating new ASP.NET web application in Visual Studio 2008.

  • Click “New Project” icon or Press “Ctrl+Shift+N”
  • Select “ASP.NET Web Application” and name the application as “ADO.NETSrv”
  • Click “OK” button
  • Go to the properties of “ASP.NET Web Application”
  • Select “Web” tab
  • Select “Specific” port as the screenshot below (Visual Studio uses the dynamic port for ASP.NET by default. But using the dynamic port will give us the trouble “Cross-domain issue” since we are going to hard-code the service URL in our silverlight project. So, we just make sure our site to run with static port.)Making the static port in Visual Studio 2008

This is about creating ASP.NET web application in Visual Studio 2008. We will create “ADO.NET Entity Data Model” in next step.

Creating ADO.NET Entity Data Model

  • Add new item “ADO.NET Entity Data Model” to our web application
    ADO.NET Entity Data Model
  • Click “OK” button (Entity Data Model Wizard will be shown as the picture below).
  • Select “Generate from Database”Model Generate from Database
  • Click “Next” button
  • Choose Your Data Connection

    Choose Your Data Connection

  • Click “Next” button
  • You have to choose your table in that step. (Customer table in my case)
    Choose Your Database Objects
  • Click “Finish” button (Visual Studio will create Model1.edmx in your project.)
    Customers Table

That’s all about creating “ADO.NET Entity Data Model” in Visual Studio 2008. We will create “ADO.NET Data Service” in next step.

Creating ADO.NET Data Service

  • Add “ADO.NET Data Service” to your web application (Note : If you don’t have ASP.NET 3.5 extension installed in your machine, you won’t see this template.)

    ADO.NET Data Service

  • Click “Add” button
  • Open “WebDataService1.svc.cs” file. ( You will see “WebDataService< /* TODO: put your data source class name here */ >” in WebDataService1 class.)
  • Write your data source class name in that place. ( WebDataService<EmployeeMgmtEntities> in my case.) If you don’t see the class in your intellisense, add your data model namespace (e.g. using EmployeeMgmtModel;) in that class. (if you don’t know what your data model namespace, you can check it in “Model Browser” )
  • Write the following code in InitializeService method
    config.SetResourceContainerAccessRule("*", ResourceContainerRights.All);
    

That’s all about creating ADO.NET Data Service in Visual Studio. You can run the project now. It will show you like Feed. So, if you want to see the XML, you need to disable the “Feed Reading View”. Go to “Internet Options”>Content>Feeds Setting. Uncheck “Turn on feed reading view”.

“Turn on feed reading view”.

Then, you can run the application. You will see the data as XML format in browser. You can query the data from url like http://localhost:49970/WebDataService1.svc/Customers or http://localhost:49970/WebDataService1.svc/Customers(1001)/ in address bar. (1001 is the id of first customer.) This is one cool thing about ADO.NET Data service. If you want to know more about this query, you can read here. Okay!!. We will create the Silverlight project in next step.

Creating Silverlight project with ADO.NET Data Service Silverlight Addon

  • Add the Silverlight project to your solution
  • Add “Microsoft.Data.WebClient.dll” as a reference in your silverlight project
  • Add the SilverlightLink to Web application

    Add Silverlight Link

  • Copy TestPage.html, TestPage.html.js and Silverlight.js to Web Application
  • Set TestPage.html as a startup page
  • Create the class like below. (Note: The class name should be the same as your table name. The fields should be the same as your table structure.)
     
    public class Customer {
    public int CustomerID { get; set; }
    public string CustomerName { get; set; }
    }
    

    I don’t like to create that class in Silverlight project but this is by-design issues so that I have no choice. I hope Astoria Team will do something about it.

  • Write the following code in Page_Load event to invoke the ADO.NET Data Service from Silverlight
    try {
    WebDataContext ctx = new
    WebDataContext("http://localhost:50527/WebDataService1.svc");WebDataQuery<Customer> customers = ctx.CreateQuery<Customer>("/Customers?$orderby=CustomerID");
    
    foreach (Customer c in customers) {
    Console.WriteLine(c.CustomerID + "," + c.CustomerName);
    }
    }
    catch (Exception ex) {
    Console.WriteLine(ex.Message);
    }
    

You can set the breakpoint in foreach loop and run the application. You will get all records from your database. but how we can show the data in Silverlight?

Silverlight ListView

There is no Gridview or ListView in Silverlight application. So, we have to create our own control for that. Based on the design of Microsoft Download Beta, I have created one simple Silverlight ListView to show the data in Silverlight application.

Let’s try to check how our listview looks like. We have to comment all codes in Page Load event and write the following code to show the ListView. Then, run the application.

ListView _listview = new ListView();
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.Items.Add(new ListViewItem("", ""));
_listview.UpdateView();
this.Children.Add(_listview);

Great! our listview looks cool.

Silverlight ListView

then, update the code like below in Page load event. And run the application. All records from Database will be shown in our ListView through ADO.NET Data Service.

try {

WebDataContext ctx = new
WebDataContext("http://localhost:50527/WebDataService1.svc");

ListView _listview = new ListView();

WebDataQuery<Customer> customers = ctx.CreateQuery<Customer>("/Customers?$orderby=CustomerID");

foreach (Customer c in customers) {
Console.WriteLine(c.CustomerID + "," + c.CustomerName);
_listview.Items.Add(new ListViewItem(c.CustomerID.ToString(), c.CustomerName));
}

_listview.UpdateView();

this.Children.Add(_listview);

That’s all about consuming the ADO.NET Data Service from Silverlight application. Even though this article doesn’t cover any details about ADO.NET Data Service, I hope you will get some understanding about how to use that service in Silverlight application. This is the purpose of writing this article. Hopefully, you enjoy reading it. Feel free to drop a comment if you have any suggestion or comment. Thanks.

 原文地址:http://michaelsync.net/2008/01/15/consuming-adonet-data-service-astoria-from-silverlight

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章