View
217
Download
1
Tags:
Embed Size (px)
Citation preview
Microsoft Silverlight Applications Need Data...and Here's How to Get It!Gill CleerenMicrosoft Regional Director Benelux / MVP ASP.NET.NET Software architect Ordina Belgiumwww.snowball.be
About Gill
• .net architect Ordina (ordina.be) • Microsoft Regional Director (theregion.com) • MVP ASP.net• Writing:
.net magazineBlogs MSDN
• Speaking: TechDaysUsergroups(Visug, Biwug, Besug)Ineta speaker
• Blog: www.snowball.be • Email: [email protected]• Twitter: gillcleeren • MSN: [email protected]
Agenda Silverlight and data• Building a service for Silverlight• Accessing WCF services• Accessing REST services• Accessing RSS feeds
Applications interact with the outside world
Product catalog
Search stringProduct
databaseProduct information
In-Browser Application
PublicInternetmashup APIs
ExistingIntranetservices
New servicesyou build
RSS/AtomFeeds
ImagesSoundsVideos
Many Types of Outside Data
A Bit of History: Silverlight 1.0
PublicInternetmashup APIs
ExistingIntranetservices
New servicesyou build
RSS/AtomFeeds
ImagesSoundsVideos
JavaScript
HTML
?
AJAX (XmlHttpRequest)
1.0
<XAML/>
2
Managed Code (C#/VB)
The Story Today: Silverlight 2
PublicInternetmashup APIs
ExistingIntranetservices
New servicesyou build
RSS/AtomFeeds
ImagesSoundsVideos
HTML
New servicesyou build
Question 2:
What is the recommended way to create services for Silverlight?
Recap: Required Steps
WCFService
• 1. Create the Service• 2. Define what it does
• 3. “Add Service Reference”• 4. Use the Service!
Creating a Service for Silverlight“Add New Item” (in Web Site / Web App) “Silverlight-Enabled WCF Service”
“Add New Item” “WCF Service”• Change wsHttpBinding basicHttpBinding in config
basicHttpBinding <endpoint contract=“IShoppingService” binding=“wsHttpBinding”…>
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!
Adding a ReferenceIn the Silverlight project: “Add Service Reference”
• “Discover” button will find services in solution• Can also give external URL
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
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<GetProductsCompletedEventArgs>(proxy_GetProductsCompleted);
proxy.GetProductsAsync(“book”);
void proxy_GetProductsCompleted(object sender, GetProductsCompletedEventArgs e){
// Process response…}
Data Binding to Services
• All generated types/collections support data binding• Future Possibility:
Advanced data binding to services (XAML-only)
E.g. <GetProductsDataSource />
What we covered so far…
2
PublicInternetmashup APIs
ExistingIntranetservices
New servicesyou build
RSS/AtomFeeds
ImagesSoundsVideos
But what about the client code for other service types?
Services that describe themselves
SOAPservices in the enterprise
Services for your Silverlightproject
SQL ServerData Services*(Astoria)
SOAP serviceson the Internet
Computer-ReadableMetadata
(e.g. WSDL)
AutomaticProxy
Generation
WCF
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…
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?
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!
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>
Services that don’t describe themselves
RESTServices
“Mashup APIs” “Web APIs”
JSONServices
“POX”(Plain Old XML)services
Human-ReadableDocumentation
SomeManual
Work Required
1. Build a URL2. Make a request3. Work with request/response data (XML or JSON)
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…
Manually Issuing Requests
• Build a URLWhat are the allowed protocols?Where can I connect to?
• Make a RequestHow do I make a request?
• Working with Request/Response DataHow do I work with XML?How do I work with JSON?
Manually Issuing Requests
• Build a URLWhat are the allowed protocols?Where can I connect to?
• Make a RequestHow do I make a request?
• Working with Request/Response DataHow do I work with XML?How do I work with JSON?
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
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?
Making HTTP Requests
• WebClient• Simple to use• Limited functionality
• HttpWebRequest• Access to all features
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?
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")) { //... } }
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}}
Consuming Feeds
Atom 1.0Feeds
RSS 2.0Feeds
AtomPublishing(Future?)
Conform to a Standard
Built-in classes
to work withsuch services
SyndicationFeed feed = SyndicationFeed.Load(…)
foreach (SyndicationItem item in feed){ //Do something with item}
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
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
Microsoft Silverlight Applications Need Data...and Here's How to Get It!Gill CleerenMicrosoft Regional Director Benelux / MVP ASP.NET.NET Software architect Ordina Belgiumwww.snowball.be
© 2009 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market
conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.