Download presentation
Presentation is loading. Please wait.
1
Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium www.snowball.bewww.snowball.be - www.codeflakes.netwww.codeflakes.net gill.cleeren@ordina.be Working with Data and Web Services in Silverlight 2
2
Get to know the audience… Is this Silverlight?
3
Agenda Introduction Building our first Silverlight/WCF application Services that describe themselves Services that don’t describe themselves Accessing RSS/Atom feeds Summary
4
Applications interact with the outside world Product catalog Search string Product database Product information
5
In-Browser Application Public Internet mashup APIs Existing Intranet services New services you build RSS/Atom Feeds Images Sounds Videos Many Types of Outside Data
6
A Bit of History: Silverlight 1.0 Public Internet mashup APIs Existing Intranet services New services you build RSS/Atom Feeds Images Sounds Videos JavaScript HTML ? AJAX (XmlHttpRequest) 1.0
7
2 Managed Code (C#/VB) The Story Today: Silverlight 2 Public Internet mashup APIs Existing Intranet services New services you build RSS/Atom Feeds Images Sounds Videos HTML
8
Question 1: What does the client code look like? 8 Managed Code (C#/VB) 2
9
New services you build Question 2: What is the recommended way to create services for Silverlight?
10
Using a Custom Service from Silverlight 10 Service Product database DEMO
11
Recap: Required Steps WCF Service 1. Create the Service 2. Define what it does 3. “Add Service Reference” 4. Use the Service!
12
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
13
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!
14
Adding a Reference In the Silverlight project: “Add Service Reference” “Discover” button will find services in solution Can also give external URL
15
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
16
Making the Call Only asynchronous calls supported Set up GetProductsCompleted event “Tab,Tab” in Visual Studio Call GetProductsAsync var proxy = new ShoppingServiceClient(); proxy.GetProductsCompleted += new EventHandler (proxy_GetProduc tsCompleted); proxy.GetProductsAsync(“book”); void proxy_GetProductsCompleted(object sender, GetProductsCompletedEventArgs e) { // Process response… } var proxy = new ShoppingServiceClient(); proxy.GetProductsCompleted += new EventHandler (proxy_GetProduc tsCompleted); proxy.GetProductsAsync(“book”); void proxy_GetProductsCompleted(object sender, GetProductsCompletedEventArgs e) { // Process response… }
17
Data Binding to Services All generated types/collections support data binding Future Possibility: Advanced data binding to services (XAML-only) E.g.
18
What we covered so far… 2 Public Internet mashup APIs Existing Intranet services New services you build RSS/Atom Feeds Images Sounds Videos
19
Approach #1: "Add Service Reference" Metadata-driven, with Intellisense 19
20
Services that describe themselves SOAP services in the enterprise Services for your Silverlight project SQL Server Data Services* (Astoria) SOAP services on the Internet Computer-Readable Metadata (e.g. WSDL) Automatic Proxy Generation WCF
21
Demo: Demo: Accessing the Live Search API from Silverlight in an automatic way 21
22
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…
23
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?
24
Cross-Domain Policy Files Silverlight looks for two policy files: Silverlight policy: clientaccesspolicy.xml Adobe Flash policy: crossdomain.xml Already used by All public services that work with Flash will also work with Silverlight!
25
25 Cross-Domain Policy Files
26
Approach #2: Write the Code Manually “A service call is just an HTTP request” 26
27
Human-Readable Documentation Only
28
Services that don’t describe themselves REST Services “Mashup APIs” “Web APIs” JSON Services “POX” (Plain Old XML) services Human-Readable Documentation Some Manual Work Required 1. Build a URL 2. Make a request 3. Work with request/response data (XML or JSON) 1. Build a URL 2. Make a request 3. Work with request/response data (XML or JSON)
29
Demo: Demo: Accessing Flickr from Silverlight 29
30
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…
31
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?
32
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?
33
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
34
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?
35
Making HTTP Requests WebClient Simple to use Limited functionality HttpWebRequest Access to all features
36
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?
37
Working with XML XmlReader/XmlWriter Linq to XML XmlSerializer static void w_DownloadStringCompleted(object sender DownloadStringCompletedEventArgs e) { XElement x = XElement.Parse(e.Result); foreach (photo in x.Elements("photo")) { //... } static void w_DownloadStringCompleted(object sender DownloadStringCompletedEventArgs e) { XElement x = XElement.Parse(e.Result); foreach (photo in x.Elements("photo")) { //... }
38
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}}
39
Approach #3: Use Built-In Classes … for RSS/Atom feeds 39
40
Consuming Feeds Atom 1.0 Feeds RSS 2.0 Feeds Atom Publishing (Future?) Conform to a Standard Built-in classes to work with such services SyndicationFeed feed = SyndicationFeed.Load(…) foreach (SyndicationItem item in feed) { //Do something with item } SyndicationFeed feed = SyndicationFeed.Load(…) foreach (SyndicationItem item in feed) { //Do something with item }
41
Demo: Accessing my blogs RSS feeds from Silverlight 41
42
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
43
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
44
Resources Silverlight: www.silverlight.net silverlight.net/blogs/jesseliberty/ timheuer.com/blog/ www.silverlightcream.com Personal blogs: www.snowball.be www.codeflakes.net Email: gill.cleeren@ordina.be
45
Q&(maybe)A
46
Gill Cleeren Microsoft Regional Director Microsoft MVP ASP.NET Ordina Belgium www.snowball.bewww.snowball.be - www.codeflakes.netwww.codeflakes.net gill.cleeren@ordina.be Thank you!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.