Overview
Developing Silverlight applications may not always be entirely intuitive to experienced
ASP.NET developers. A Silverlight application executes within a plug-in environment
and, as such, there are no automatic postbacks to a server that originate from a Silverlight
application. If a call to a server is necessary from a Silverlight application, the call must
be manually initiated by the developer.
Sockets Programming
Silverlight supports sockets programming through the System.Net.Sockets namespace. A
socket is a low-level communication channel that is generally configured using TCP/IP.
Silverlight supports asynchronously sending data back and forth across a socket over
ports ranging from 4502 to 4534. Silverlight supports cross-domain socket
communications between a Silverlight application and any server, provided that a special
security policy file is in place on the server. (see footnote)
Sockets are most commonly used to push data from the server to a client. A socket, when
used in this manner, creates a client-server relationship based on the publisher-subscriber
paradigm. This eliminates the need to have the client periodically poll the server for
updates, which may (or may not) have occurred.
Serialization
1
simple text. But then how do more complex items such as images, graphs, animations,
and multimedia get transported from a Web server to a client? Such items are first
converted to text, then transported over the Web, and then recreated from the text
representation.
The process of converting an item from a serialized, text representation back to the
original object is called deserialization.
The most common formats for an item to be converted to for transportation over the Web
are XML, SOAP, and JSON. The .NET Framework and Silverlight both include facilities
for serializing and deserializing data.
Web Services
The general steps to build a Web service that can be consumed by a Silverlight
application are as follows:
1. When creating a new Silverlight project in Visual Studio, be sure to select the
option to add a new Web to the solution. The new Web project that is created
serves as a test harness project for the Silverlight application.
2. A Web service will not exist in the new Web by default but can easily be added
by right-clicking the Web project and selecting Add New Item. From the Add
New Item dialog, select Web Service (if you are supporting .NET 2.0 ASMX
Web Services), or Silverlight-Enabled WCF Service (if you are supporting .NET
3.5 on the target web server). Note that Silverlight 2 can use either ASMX or
WCF Web Services, but there are restrictions on what WCF functionality you can
use from Silverlight 2 because only the Basic HTTP Binding is supported.
3. The figure below illustrates the project as displayed in the Solution Explorer once
the new Silverlight project is created and a new ASMX Web service is added to
the project. The example illustrated here will become a Web service that will read
weather forecast data from a database.
2
3
The entity relationship diagram for the Forecast database that is used by the Web service
is shown in the figure below.
The example Web service returns a generic list of forecast objects. In order for a class to
be able to be serialized, transported over the Web, and then deserialized, the class must
contain a default constructor and must be marked up by using the [Serializable] attribute.
Once the Web service has been created and tested, add a Service Reference to the
Silverlight project so that the Silverlight project can consume the Web service. Complete
coding the control and integrate feedback from the Web service to complete the control.
Be aware that as of Silverlight, only asynchronous calls can be made to any type of
service. Hence, a small amount of additional coding and planning is required to
accommodate for the asynchronous processing. The resulting Silverlight control is shown
in the figure below.
4
Windows Communication Foundation
Beyond implementing an interface and custom types, the process of developing a WCF
service is almost identical to developing a Web service. The example service created
above can be replicated by using a WCF service with minimal differences.
5
Other Networking Protocols
Silverlight supports the use of other communication protocols for use in communicating
with and consuming services and Web resources.
Plain Old XML (POX), or simple, well-formed, and valid XML documents and XML
fragments can be programmatically sent and received over the Web to enable
communication with services. In most cases, passing data over the Web by using POX
will require more coding for the developer but offers the developer the ability to fully
customize the schema of the XML being sent. Additionally, there may be scenarios
where a service being consumed does not fully support all Web standards and XML must
be used to exchange data with the service.
6
HTTP is an acronym for Hypertext Transport Protocol and is the standard protocol used
to communicate over the Web. HTTP consists of request and response messages that are
transported over the Web between computers with one computer acting as a client and the
other computer acting as a server. HTTP messages are referred to as HTTP packets and
contain a head section and a body section. The head section of an HTTP packet contains
information that is used by the application that receives and processes the HTTP packet
while the body section contains the data that is being transported using HTTP such as an
HTML document. The contents of the body section is referred to as the "payload". The
contents of the payload may include an HTML or XML document and, in turn, typically
contains subsections of its own, such as contents to display on an HTML document or
scripts.
REST was designed by the same people that designed HTTP and it eliminates the extra
layer of transport protocol. The head section of an HTTP can contain commands and
instructions such as GET or POST that are read and executed by the application that
processes the packet. The commonly-used HTTP commands are GET, POST, PUT, and
DELETE. These commands are analogous to the SQL DML commands that are also
referred to as NURD (new, update, read, delete) or CRUD (create, read, update, delete).
The POST command is used to issue create, update, or delete commands. The GET
command is used to issue a read command. The PUT command is used to issue create or
overwrite commands. The DELETE command is used to issue a delete command.
Through the aforementioned commands, REST supports code on demand and it also
supports embedded hyperlinks.
Applications that communicate over the Web by using REST are referred to as
"RESTful" applications. REST also eliminates, or extremely reduces, the need to manage
state. The Web is, by nature, a stateless environment. When a server receives an HTTP
request, it fulfills the request and returns an HTTP response. Once the response is sent,
the server forgets about the request and moves on to the next request. The server's lack of
ability to remember who a client is between calls is referred to as stateless. The ability for
a server to remember clients between requests is referred to as managing state. In most
modern Web development technologies, servers manage state by using cookies. The
REST standard discourages the use of cookies due to security and navigation precautions.
Syndication Protocols
7
In addition to supporting standard communication protocols, Silverlight also supports the
two most commonly-used syndication protocols, RSS and ATOM.
RSS documents can be easily and automatically updated once created and, typically, have
a .rss file extension. Just as a Web browser reads and parses HTML documents, an RSS
feed reader, or news reader, reads and parses RSS documents. The primary benefit of
using RSS is that users can subscribe to an RSS feed to be notified when new content is
published. Several versions of RSS have been released but the current official version is
version 2.0. The markup listing below illustrates an RSS document:
• The root <rss> element contains a collection of channel <channel> elements with
each defining a separate RSS syndication source.
• Each channel must include the title of the channel, the link to the channel, and a
description of the channel.
• Each channel <channel> element contains a collection of <item> item elements
with each defining a single item to be published.
8
• Each item must include the title of the item, the link to the item, and a description
of the item.
• An RSS feed reader, also known as an aggregator, can read the document and
provide updated information with links to the user as the RSS document is
updated.
ATOM
A problem with RSS is that several different and incompatible versions exist. In an effort
to resolve the incompatibilities and inconsistencies between the various versions of RSS,
a new syndication standard named Atom was created. Atom is divided into two
technologies, the Atom Syndication Format and the Atom Publishing Protocol.
The Atom Syndication Format is a custom XML grammar that serves much the same
purpose as the RSS XML grammar. An Atom Syndication Format file is a XML file with
a .atom file extension. The markup listing below illustrates the Atom version of the pet
blog entry:
Code Explanation
The Atom Publishing Protocol is a query language similar to the Structured Query
Language (SQL) designed for use with the Atom Syndication Format. The Atom
Publishing Protocol consists of four primary commands: GET, POST, PUT, and
DELETE, analagous to the SQL SELECT, INSERT, UPDATE and DELETE statements.
9
Windows Azure
Cloud Computing refers to internet-based resources are provided in a highly scalable
service. This can save companies money because they do not have to invest in expensive
hardware to provide high scalability. Also, maintenance and power costs can be greatly
reduced. The Azure Services Platform is Microsoft's Cloud Computing solution.
Live Search
10
Exercise: Create the Athlete Database
Duration: 20 to 30 minutes.
In this exercise, you will create a new SQL Server 2005 database that contains at least
three tables that will store athlete information. The steps in the exercises in this lab will
guide you to build a simulated athlete management system using Silverlight. If you would
like, and you have adequate experience, feel free to modify and customize the lab steps to
your liking.
Duration: 5 to 10 minutes.
1. Add a new class to the Web project (not the Silverlight project).
2. Design the class to contain athlete. Mark up the class using the Serializable
attribute. The code for the example Athlete class is shown below.
using System;
/// <summary>
/// Summary description for Athlete
/// </summary>
[Serializable]
public class Athlete
{
#region Data members
// data members.
public int AthleteId { get; set; }
public int SportId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
11
public string Phone { get; set; }
public string Email { get; set; }
public string Website { get; set; }
public string Photo { get; set; }
public int Height { get; set; }
public int Weight { get; set; }
public string Biography { get; set; }
public string Notes { get; set; }
#endregion
#region Constructors
public Athlete() { }
12
this.Height = height;
this.Weight = weight;
this.Biography = biography;
this.Notes = notes;
}
#endregion
}
Duration: 5 to 10 minutes.
1. Add a new class to the Web project (not the Silverlight project).
2. Design the class to contain sport information. Mark up the class using the
Serializable attribute. The code for the example Sport class is shown below.
using System;
/// <summary>
/// Summary description for Sport
/// </summary>
[Serializable]
public class Sport
{
#region Data members
// data members.
public int SportId { get; set; }
public string Name { get; set; }
#endregion
#region Constructors
public Sport() { }
Duration: 30 to 45 minutes.
In this exercise, you will create a Web service that uses the classes and database created
previously. This service will be used by the Silverlight application.
1. Add a new Web service (or WCF service, if desired) to the Web project (not the
Silverlight project).
13
2. Add methods to the Web service code file for CreateAthlete, CreateUser,
ReadAthletes, ReadAthleteById, ReadSports, and AuthenticateUser. The code for
the example AthleteService is shown below.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Security;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
/// <summary>
/// Summary description for AthleteService
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET
AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class AthleteService : System.Web.Services.WebService
{
SqlConnection cn = new
SqlConnection(ConfigurationManager.ConnectionStrings["athleteDB"].Conne
ctionString);
public AthleteService()
{
[WebMethod(EnableSession = true)]
public string HelloWorld()
{
if (!IsLoggedIn())
throw new Exception("User is not logged in!!!");
else
return "Hello World!";
}
[WebMethod(EnableSession = true)]
public bool AuthenticateUser(string username, string password)
{
14
bool valid = Membership.ValidateUser(username, password);
if (valid)
{
FormsAuthentication.SetAuthCookie(username, false);
Session["username"] = username;
}
else
Session["username"] = null;
return valid;
}
[WebMethod]
public List<Athlete> ReadAthletes()
{
List<Athlete> athletes = new List<Athlete>();
cn.Open();
if (rdr.HasRows)
{
Athlete athlete;
while (rdr.Read())
{
athlete = new Athlete();
athlete.AthleteId =
int.Parse(rdr["AthleteId"].ToString());
athlete.FirstName = rdr["FirstName"].ToString();
athlete.LastName = rdr["LastName"].ToString();
athlete.Address = rdr["Address"].ToString();
athlete.City = rdr["City"].ToString();
athlete.State = rdr["State"].ToString();
athlete.Zip = rdr["Zip"].ToString();
athletes.Add(athlete);
}
}
}
cn.Close();
cn.Dispose();
return athletes;
}
[WebMethod]
public void SaveAthlete(Athlete athlete)
{
15
if (athlete.AthleteId > 0)
sqlText = "update Athletes set FirstName=@FirstName,
LastName=@LastName, Address=@Address, City=@City, State=@State,
Zip=@Zip where AthleteId = @AthleteId";
else
sqlText = "insert into Athletes (FirstName, LastName,
Address, City, State, Zip) values (@FirstName, @LastName, @Address,
@City, @State, @Zip)";
cn.Open();
[WebMethod]
public void DeleteAthlete(Athlete athlete)
{
string sqlText = "delete from Athletes where AthleteId =
@AthleteId";
cn.Open();
16
Due to limited time to complete these modules and exercises, you can
create user accounts by testing the Web service and invoking the
CreateUser Web method.
Duration: 5 to 10 minutes.
In this exercise, you will add a service reference to the Silverlight project.
1. The Silverlight project must be able to deserialize objects as they are sent from
the Web service. Hence, a copy of the object must be available for the Silverlight
application to inspect. When a service reference is created for the Web project in
the Silverlight project, any custom classes are made available to the Silverlight
project.
2. Right-click the Silverlight project in the Solution Explorer and select Add Service
Reference.
3. From the Add Service Reference dialog, select the Discover button to discover
services in the current solution. Assign a name to the service and click OK.
4. Before we can fully use the database and Web service that are in place, we must
build some classes and controls for the Silverlight application. In a future lesson
("Securing Silverlight Applications"), we will build a class that will be used to
track a user's security credentials and login status.
Duration: 30 to 45 minutes.
In this exercise, you will use Silverlight to create a simple RSS reader. Feel free to extend
the exercise to improve the lab in ways that you see fit.
17
entries as they are acquired. In the sample application, a ScrollViewer control was
added and a TextBlock control was added to the ScrollViewer as shown in the
figure below.
7. Once you have completed designing the Silverlight control, close Expression
Blend and return to Visual Studio.
8. In the Page.xaml code behind, in the Page constructor, just after the
InitializeComponent() method call, add code to create a new HttpWebRequest to
the RSS URL as shown in the code snippet below:
9. Next, just after that line, add a line of code that asynchronously acquires the feed
and assigns a callback handler as shown in the snippet below:
httpRequest.BeginGetResponse(new AsyncCallback(HttpResponseHandler),
httpRequest);
10. Create the HttpResponseHandler callback method in the code behind file as
shown in the code snippet below:
18
HttpWebResponse httpResponse =
(HttpWebResponse)httpRequest.EndGetResponse(result);
11. Keep in mind when entering the URL for an RSS feed to acquire that the server
hosting the feed must opt-in to cross-domain access by providing a
Clientaccesspolicy.xml or Crossdomain.xml file at the root of the domain.
12. Press F5 to test the application.
Duration: 30 to 45 minutes.
In this exercise, you will use Silverlight to call the Live Search component of the Azure
Services Platform.
19
4. In the LiveSearchAzure.Web project, right-click and select "Add Service
Reference" and then enter the following URL, where MY_APP_ID is the API ID
that you were given in step 1. Name the Web Reference "LiveSearchService"
http://api.search.live.net/search.wsdl?MY_APP_ID
5. In the Web Project, Create a new Silverlight-Enabled WCF Web Service and
name it LiveSearchWrapper.svc
6. At the top of the LiveSearchWrapper.svc.cs file, add a using statement for the
proxy namespace:
using LiveSearchAzure.Web.LiveSearchService;
7. In the Web Service, Add a new WebMethod to search for Web Pages, and a new
Class to store and return results. IMPORTANT: Be sure to replace MY_APP_ID
with your Live Search API ID from Step 1.
[OperationContract]
public List<SearchResult> DoSearch(string search)
{
LiveSearchService.LiveSearchPortTypeClient s = new
LiveSearchPortTypeClient();
request.Version = "2.0";
request.Market = "en-us";
request.Adult = AdultOption.Moderate;
request.AdultSpecified = true;
20
}
<Grid.RowDefinitions>
<RowDefinition Height="25" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal">
<TextBox x:Name="txtSearch" Width="200" />
<Button x:Name="btnSearch" Content="Search" />
</StackPanel>
21
11. Add code to call the web service DoSearch method asynchronously:
12. Run the application and try entering a search term, then click the Search button.
22