Tải bản đầy đủ (.pdf) (16 trang)

Data Access and Networking

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (220.86 KB, 16 trang )

C H A P T E R 6

■ ■ ■

137


Data Access and Networking
Data access in Silverlight applications works differently than it does in traditional applications. You’ll
need to be aware of how it works and the limitations. In this chapter, you will look at what makes data
access different, and then explore mechanisms for accessing data in a Silverlight application.
Data Access in Silverlight Applications
As discussed in Chapter 1, RIAs bridge the gap between Windows-based smart clients and web-based
applications. When moving to this type of environment, data access and networking can be confusing.
In a Windows-based smart client, the application has access to the database at all times. It can
create a connection to the database, maintain state with the database, and remain connected.
On the other hand, a web application is what is known as a pseudo-conversational environment,
which is, for the most part, a completely stateless and disconnected environment. When a client makes a
request to the web server, the web server processes the request and returns a response to the client. After
that response has been sent, the connection between the client and the server is disconnected, and the
server moves on to the next client request. No connection or state is maintained between the two.
In Silverlight applications, we have one additional layer of complexity. The application runs from
the client’s machine. However, it is still a disconnected environment, because it is hosted within a web
browser. There is no concept of posting back for each request or creating a round-trip to the server for
data processing. Therefore, data access is limited to a small number of options.
In addition, a Silverlight application has a number of security restrictions placed on it to protect
the users from the application gaining too much control over their machine. For instance, the
Silverlight application has access to only an isolated storage space to store its disconnected data. It has
no access whatsoever to the client’s hard disk outside its “sandbox.” Silverlight’s isolated storage is
discussed in more detail in Chapter 9.
What are your options for accessing data in a Silverlight application? The following main


mechanisms are available:
• The most common mechanism to access data from a Silverlight application is
through web services, typically a WCF service.
• Silverlight applications can access data using ADO.NET Data Services, which
provides access to data through a URI syntax.
• Silverlight also has built-in socket support, which allows applications to connect
directly to a server through TCP sockets.
• Silverlight has out-of-the-box support for JavaScript Object Notation (JSON), as
well as RSS 2.0 and Atom 1.0 syndication feed formats.
CHAPTER 6 ■ DATA ACCESS AND NETWORKING

138

Of these mechanisms, I’ll explore accessing WCF services from Silverlight 2 in depth, and then
have a high-level look at using sockets. For examples and more information on accessing other data
services, refer to Pro Silverlight 3 in C# 2008 by Matthew MacDonald (Apress, 2009).
Accessing Data Through Web Services
One of the ways that a Silverlight application can access data is through web services. These can be
ASP.NET Web Services (ASMX), Windows Communication Foundation (WCF) services, or
representational state transfer (REST) services. Here, you will concentrate on using a WCF service,
which is the preferred way of accessing data in a Silverlight application through web services.
Try It Out: Accessing Data Through a WCF Service
To demonstrate accessing data from a WCF service, you will build the same application that you built
in Chapter 5 to try out the DataGrid. (For more information about any part of this exercise regarding
the DataGrid, refer back to Chapter 5.) The difference will be that the application will get the data
through a web service.
As you’ll recall, this application displays common starting hands in poker and the nicknames that
have been given to those starting hands. The UI will have three columns: the first column will display
two images of the cards in the hand, the second column will display the nickname, and the third
column will contain notes about the hand. The completed application is shown in Figure 6-1.


Figure 6-1. The poker starting hands application
1. Create a new Silverlight application in Visual Studio 2008. Call the
application Ch6_WCFService, and allow Visual Studio to create a Web
Application project named Ch6_WCFService.Web to host your application, as
shown in Figure 6-2.
CHAPTER 6 ■ DATA ACCESS AND NETWORKING

139


Figure 6-2. Adding the Silverlight application hosting project
Right-click the Ch6_WCFService.Web project and select Add  Class. Name the
new class StartingHands.cs, as shown in Figure 6-3.

Figure 6-3. Adding the StartingHands.cs class to the project
CHAPTER 6 ■ DATA ACCESS AND NETWORKING

140

2. Now you need to implement the StartingHands.cs class. It is very similar to
the class used in Chapter 5’s DataGrid example. To save yourself some typing,
you can copy the code from that project. As shown in bold in the following
code, the only differences are the namespace and the return type of the
GetHands() method. Instead of using an ObservableCollection, it will return a
simple List<StartingHands>.
■ Note In a real-world example, the
StartingHands.cs
class would be doing something like retrieving data from
a SQL Server database and executing some business logic rules on the data. For simplicity, this example just

returns a static collection.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace Ch6_WCFService.Web
{
public class StartingHands
{
public string Nickname { get; set; }
public string Notes { get; set; }
public string Card1 { get; set; }
public string Card2 { get; set; }

public static List<StartingHands> GetHands()
{
List<StartingHands> hands = new List<StartingHands>();

hands.Add(
new StartingHands()
{
Nickname = "Big Slick",
Notes = "Also referred to as Anna Kournikova.",
Card1 = "As",
Card2 = "Ks"
});

hands.Add(
new StartingHands()

{
Nickname = "Pocket Rockets",
Notes = "Also referred to as Bullets.",
Card1 = "As",
Card2 = "Ad"
});

hands.Add(
CHAPTER 6 ■ DATA ACCESS AND NETWORKING

141

new StartingHands()
{
Nickname = "Blackjack",
Notes = "The casino game blackjack.",
Card1 = "As",
Card2 = "Js"
});

hands.Add(
new StartingHands()
{
Nickname = "Cowboys",
Notes = "Also referred to as King Kong",
Card1 = "Ks",
Card2 = "Kd"
});

hands.Add(

new StartingHands()
{
Nickname = "Doyle Brunson",
Notes = "Named after poker great Doyle Brunson",
Card1 = "Ts",
Card2 = "2s"
});


return hands;
}
}
}
3. Next, you need to add the WCF service that will call the
StartingHands.GetHands() method. Right-click the Ch6_WCFService.Web pr oject
and select Add ~TRA New Item. In the Add New Item dialog box, select the
template named “Silverlight-enabled WCF Service” and name it
StartingHandService.svc, as shown in Figure 6-4. Then click the Add button.
CHAPTER 6 ■ DATA ACCESS AND NETWORKING

142


Figure 6-4. Adding the Silverlight-enabled WCF service
4. This will add a service named StartingHandService.svc to the project with an
attached code-behind file name d StartingHandService.svc.cs. View that code
behind. You will see that Visual Studio has already created the base WCF
service, including a sample method called DoWork(), as fol lows:
namespace Ch6_WCFService.Web
{

[ServiceContract(Namespace = "")]
[AspNetCompatibilityRequirements(RequirementsMode =
AspNetCompatibilityRequirementsMode.Allowed)]
public class StartingHandService
{
[OperationContract]
public void DoWork()
{
// Add your operation implementation here
return;
}

// Add more operations here and mark them
// with [OperationContract]
}
}

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×