46
Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium www.snowball.be - www.codeflakes.net [email protected] Working with Data and Web Services in Silverlight 2

Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium - [email protected]

  • View
    214

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Gill CleerenMicrosoft Regional DirectorMicrosoft MVP ASP.NETOrdina Belgiumwww.snowball.be - www.codeflakes.net [email protected]

Working with Data and Web Servicesin Silverlight 2

Page 2: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Get to know the audience…

Is this Silverlight?

Page 3: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Agenda

• Introduction• Building our first Silverlight/WCF application• Services that describe themselves• Services that don’t describe themselves• Accessing RSS/Atom feeds• Summary

Page 4: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Applications interact with the outside worldProduct catalog

Search stringProduct

databaseProduct information

Page 8: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Question 1:

What does the client code look like?

Managed Code (C#/VB)

2

Page 12: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Creating a Service for Silverlight“Add New Item” (in Web Site / Web App)

“Silverlight-Enabled WCF Service”

Temporary for Beta1:• “Add New Item” “WCF Service”• Change wsHttpBinding basicHttpBinding in config

basicHttpBinding <endpoint contract=“IShoppingService” binding=“wsHttpBinding”…>

Page 13: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Defining the Contract• [ServiceContract] for the service class (interface in

Beta1)• [OperationContract] for methods (in the interface in

Beta1)• [DataContract]/[DataMember] for data types[ServiceContract]

public class ShoppingService {[OperationContract]Product[] GetProducts(string searchString){ /*... Implementation ... */ }

}

[DataContract] public class Product {

[DataMember]public string description;[DataMember]public string pictureUrl;

}

Nothing Silverlight-specific

Regular WCF code!

Page 14: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Adding a ReferenceIn the Silverlight project: “Add Service Reference”• “Discover” button will find services in solution• Can also give external URL

Page 15: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Creating the Proxy

var proxy = new ShoppingServiceClient();

• Default address chosen if no parameters given• Can pass in address manually• But what if the service moves?• Configuration support after Beta1• No need to recompile Silverlight client code if service moves• Can reuse one Silverlight app for many services

Page 16: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Making the Call

• Only asynchronous calls supported• Set up GetProductsCompleted event• “Tab,Tab” in Visual Studio

• Call GetProductsAsyncvar proxy = new ShoppingServiceClient();proxy.GetProductsCompleted +=

new EventHandler<GetProductsCompletedEventArgs>(proxy_GetProductsCompleted);

proxy.GetProductsAsync(“book”);

void proxy_GetProductsCompleted(object sender, GetProductsCompletedEventArgs e){

// Process response…}

Page 17: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Data Binding to Services

• All generated types/collections support data binding• Future Possibility:

Advanced data binding to services (XAML-only)

E.g. <GetProductsDataSource />

Page 19: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Approach #1:"Add Service Reference"

Metadata-driven, with Intellisense

Page 21: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Demo:Accessing the Live Search APIfrom Silverlight in an automatic way

Page 22: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Add Service Reference

Works with:• Any “simple” SOAP service (e.g. Live Search)• SOAP 1.1 • Server-side may be JAVA, WCF, ASMX, etc.• A few restrictions (e.g. SOAP Faults not supported)

• Future Possibility: SQL Server Data Services (Astoria)

Can’t talk to just any service:Silverlight-Wide Cross-Domain Restrictions…

Page 23: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Cross-Domain Restrictions

Silverlight does not allow applications to cross domain boundaries by default:• MySite.com/silverlightApplication.xap

cannot call SomeOtherSite.com/someService.svc

• SecurityException if you try

Silverlight allows the calls if target site opts in• How do services opt in?• When should services opt-in?

Page 25: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Cross-Domain Policy Files

<?xml version="1.0" ?> <cross-domain-policy>  <allow-access-from domain="*" />   </cross-domain-policy>

<?xml version="1.0" encoding="utf-8" ?> <access-policy> <cross-domain-access> <policy>

<allow-from>  <domain uri="*" />   </allow-from>

<grant-to>  <resource path="/" include-subpaths="true" />   </grant-to>  </policy>  </cross-domain-access></access-policy>

Page 26: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Approach #2:Write the Code Manually

“A service call is just an HTTP request”

Page 27: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Human-Readable Documentation Only

Page 29: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Demo:Accessing Flickr from Silverlight

Page 30: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Manually Issuing Requests

Code was exactly as in the regular .NET Framework!Good news for existing .NET developers

Some Silverlight-specific things to be aware of…

Page 31: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Manually Issuing Requests

• Build a URL• What are the allowed protocols?• Where can I connect to?

• Make a Request• How do I make a request?

• Working with Request/Response Data• How do I work with XML?• How do I work with JSON?

Page 32: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Manually Issuing Requests

• Build a URL• What are the allowed protocols?• Where can I connect to?

• Make a Request• How do I make a request?

• Working with Request/Response Data• How do I work with XML?• How do I work with JSON?

Page 33: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Allowed URLs

• HTTP and HTTPS• Some restrictions on HTTPS, cross-scheme• A few of these will go away after Beta1

• Subject to cross-domain rules• Must have policy file if not local URL

• No ftp:// or file:// URLs

Page 34: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Manually Issuing Requests

• Build a URL• What are the allowed protocols?• Where can I connect to?

• Make a Request• How do I make a request?

• Working with Request/Response Data• How do I work with XML?• How do I work with JSON?

Page 35: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Making HTTP Requests

• WebClient• Simple to use• Limited functionality

• HttpWebRequest• Access to all features

Page 36: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Manually Issuing Requests

• Build a URL• What are the allowed protocols?• Where can I connect to?

• Make a Request• How do I make a request?

• Working with Request/Response Data• How do I work with XML?• How do I work with JSON?

Page 37: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Working with XML

• XmlReader/XmlWriter• Linq to XML

• XmlSerializer

static void w_DownloadStringCompleted(object senderDownloadStringCompletedEventArgs e) { XElement x = XElement.Parse(e.Result); foreach (photo in x.Elements("photo")) { //... } }

Page 38: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

The JSON Data Format

• “JavaScript Object Notation”• Easy and fast to parse in JavaScript in browsers

• Often no real reason to use it for SL, except…• Reusing existing services built for AJAX pages• Smaller message size

(but binary XML is a future possibility)

Example:{“Person”:{“name”:”john”,”age”:42}}

Page 39: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Approach #3:Use Built-In Classes

… for RSS/Atom feeds

Page 41: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Demo: Accessing my blogs RSS feeds from

Silverlight

Page 42: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Syndication Support in Silverlight

• Protocols• RSS 2.0, Atom 1.0• Future possibility: Atom Publishing Protocol

• Essentially the same as in .NET 3.5• SyndicationFeed, SyndicationItem, etc.• Can read / write feeds

• Subject to same cross-domain restrictions, etc.• Use HttpWebRequest/WebClient, then Syndication to parse

Page 43: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Summary: What We Covered

• Creating Services for Silverlight• Creating and consuming WCF services• Securing local services• Creating public services (safe for cross-domain)

• Accessing Services that Describe Themselves• “Add Service Reference”

• Accessing Services that Don’t Describe Themselves• WebClient / HttpWebRequest, manual work

• Accessing Feeds• RSS/Atom

Page 45: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Q&(maybe)A

Page 46: Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium  -  gill.cleeren@ordina.be

Gill CleerenMicrosoft Regional DirectorMicrosoft MVP ASP.NETOrdina Belgiumwww.snowball.be - www.codeflakes.net [email protected]

Thank you!