15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

1102 ❘ ChaPTer 38 silverliGht<br />

neTWorKing<br />

With Silverlight, networking is in some ways the same as from the .<strong>NET</strong> Framework stack, but it is also<br />

different in important aspects. You have the socket classes HttpRequest <strong>and</strong> WebClient that you are<br />

familiar with from Chapter 24. WCF (Chapter 43) <strong>and</strong> WCF Data Services (Chapter 32) are available as<br />

well. However, only asynchronous methods are available to send <strong>and</strong> receive requests to <strong>and</strong> from the<br />

network. The UI thread shouldn ’ t be blocked, as this can have a fatal effect for the user.<br />

Before Silverlight 3.0, the only networking stack that could be used from Silverlight was the one that was<br />

offered by the browser. Since Silverlight 3.0, this is different; you can use a different HTTP stack as well.<br />

One important restriction for applications running inside a browser on the client was that the client<br />

could only access networking services from the same server that the control came from. This has changed<br />

since Adobe ’ s Flash technology, but the target server must support this. The Silverlight networking stack<br />

checks to see if a Silverlight Policy File named clientaccesspolicy.xml exists on the server. If such<br />

a fi le does not exist, Silverlight also accepts the fi le crossdomain.xml , which is the same fi le used with<br />

Adobe Flash. The second fi le is not checked if the fi rst exists, <strong>and</strong> Silverlight doesn ’ t use all entries from the<br />

crossdomain.xml fi le <strong>and</strong> requires that the entire domain be marked as public if this fi le is used.<br />

A sample Silverlight Policy File, clientaccesspolicy.xml , is shown here. For the client domain<br />

www.cninnovation.com , access is granted to all subpaths. You can restrict the resources that the grant<br />

applies to, <strong>and</strong> also use an * for the domain URI to allow clients to come from anywhere.<br />

< xml version="1.0" encoding="utf - 8" ><br />

< access - policy ><br />

< cross - domain - access ><br />

< policy ><br />

< allow - from http - request - headers="*" ><br />

< domain uri="http://www.cninnovation.com"/ ><br />

< /allow - from ><br />

< grant - to ><br />

< resource path="/" include - subpaths="true"/ ><br />

< /grant - to ><br />

< /policy ><br />

< /cross - domain - access ><br />

< /access - policy ><br />

Let ’ s look at an example that uses networking stacks from<br />

Silverlight. For this, an ADO.<strong>NET</strong> Entity Data Model is<br />

created fi rst, to read <strong>and</strong> write to <strong>and</strong> from a database<br />

across the network.<br />

Creating an ado.neT entity data model<br />

The sample Silverlight control is used to write attendee<br />

registration information to a database. First, the user must<br />

select from a list of events before he can register for an event.<br />

The database EventRegistration defi nes three tables:<br />

Events, Attendees, <strong>and</strong> RegistrationCodes, which are mapped<br />

to the ADO.<strong>NET</strong> Entity Data Model entity classes Event ,<br />

Attendee , <strong>and</strong> RegistrationCode , as shown in Figure 38 - 4.<br />

figure 38-4<br />

You can read about how to create <strong>and</strong> use ADO.<strong>NET</strong> Entity Data Models in<br />

Chapter 31, “ADO.<strong>NET</strong> Entity Framework.”<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!