But web services is…
Download
Report
Transcript But web services is…
Distributed Programming
Web Services
•
•
•
•
Purpose
Examples of Web Services
Architecture
Web Services in .Net
UCN / IT - 2012
1
Why web services?
• Applications operates over different platforms
– A Java app gets data from a .NET app
– A Linux server gets data from an IBM mainframe
– A Windows server gets data from a Linux server
Application X
UCN / IT - 2012
Application Y
2
When to use web services?
• When interoperability between platforms is the important
demand. Else:
– Proprietary api’s and protocols are more efficient
– Proprietary api’s and protocols offers more features
– Proprietary api’s and protocols offers better security
• But web services is…
– an easy way to exchange data between two organizations
– an easy way to exchange data between a organization’s
different systems, i.e. stock, invoicing and dispatching
– an easy way to provide a service worldwide
UCN / IT - 2012
3
Web service design
• In the simple form a web service is a tier that wraps a
existing tier
– meaning a platform neutral interface to existing code:
?
Web
Service
Client
Web server
BT
DT
DB
Server
Web Service
Tier
– existing tiers should be used as they are, therefore no recoding
UCN / IT - 2012
4
Examples of web services
• What is possible today?
• There is many different public services:
– Amazon.com client (free, but requires signature)
– Windows Live search
– MapPoint maps & route planner
– Google (xml, but not soap)
– .....
Search for webservices here: http://www.xmethods.net/
Or with your favorite search engine
UCN / IT - 2012
5
Amazon.com web service
• Amazon.com offers product information via web service
• Why?
– To raise the sale! More than 10% are sold via WS…
UCN / IT - 2012
6
MapPoint web service
• Maps, route planning etc.
– Other providers too, i.e. Google Earth, MS Virtual Earth, Yahoo, etc.
– Sign up for MapPoint WebService: https://mappoint-css.live.com/mwssignup/
UCN / IT - 2012
7
Windows Live web service
• Execute searches & returns results
• Example from MSDN
UCN / IT - 2012
8
Ways to implement web services
• Web services can be implemented in many ways.
• And you can define your own way also.
• But if you do it in a standardlized way the chance that it will
be used is higher and it will properly be easier and cheaper
to develop.
• In these days most web services is based on either SOAP or
REST.
UCN / IT - 2012
9
SOAP and REST shortly
• SOAP was one of the first standardlized ways to define
webservices.
• It defines how to serialize data, how to send it, how to find the
service, how to scure it etc.
• As we will see SOAP is very easy to use for developers,
because the IDE’s can generate the code for communication
• But SOAP needs more resources on runtime. Parsing is more
complicated and more bytes are sent on the network.
•
•
•
•
REST is more a set of guidelines for making web services.
It based on a simple set of operations (usually similar to http)
Data is usually serialized to xml or another text format (json)
IDE’s cannot (as far as I know) automatically generate code
for proxies
UCN / IT - 2012
10
Two different ways of thinking
• SOAP is in many ways similar to RPC.
• You get a remote object that you can perfom a unlimited set
of operations on
• And, if you are not careful, you forget that is a remote object
and handles it as a local object.
• In SOAP the state is typically kept on the server
• REST is similar to the classic web protocols (http, ftp,..)
• Basically you handle data sets in the same way as files with
a very limited set of operations.
• So, in loose terms, you have a data set server instead of a
file server.
• In REST the state is always kept on client.
• Sum up:
– SOAP: Think in terms of objects
– REST: Think in terms of ”web”
UCN / IT - 2012
11
• Demo of two clients that do the same task, but uses web
services based on resp. SOAP and REST.
• After the demo, we’ll look a little more on SOAP.
• REST is presented more detailed in the next presentation
UCN / IT - 2012
12
A little live demo: Valuta conversion
• There is a SOAP webservice here:
– http://www.webservicex.net/CurrencyConvertor.asmx
UCN / IT - 2012
13
What happened…
•
•
•
•
Accessed systems on other places on the internet
Accessed systems running on other platforms
Went through a number of firewalls
Received non-trivial datatypes
• … all together programmed in traditional OO.
static void Main(string[] args)
{
WSCurrency.CurrencyConvertor cc = new WSCurrency.CurrencyConvertor();
Console.WriteLine("From EUR to DKK: {0}",cc.ConversionRate(WSCurrency.Currency.EUR
,WSCurrency.Currency.DKK));
Console.ReadLine();
}
UCN / IT - 2012
14
Basic architekture
• Standard RPC, but with use of XML & web server:
<Add>
<x>20</x>
<y>99</y>
</Add>
obj = new WebService();
result = obj.Add(20, 99);
(1) XML
Service
Page
Client
obj
(2) XML
Web server
<Add>
<result>119</result>
</Add>
UCN / IT - 2012
int Add(int x, int y)
{ return x + y; }
15
More details…
• Proxy and stub objects supports RPC
• Messages in SOAP format
– SOAP = Simple Object Access Protocol
Client
Service
Page
(stub)
method call
proxy
method call
obj
Web server
SOAP msg (XML)
HTTP request
UCN / IT - 2012
16
REST based client: Valuta Conversion
• There is a REST based service here:
– http://currencies.apps.grandtrunk.net/
• To find the conversion rate DKK to EUR, try the following url
in the browser:
– http://currencies.apps.grandtrunk.net/getlatest/dkk/eur
• Demo: Implement it in C#
UCN / IT - 2012
17
UCN / IT - 2012
18
What happened
•
•
•
•
•
Invoked an operation by passing a simple url.
Could do in the browser and our own client
Got a simple text based value back.
It might have been XML that was returned.
In that case it would have been necessary to parse the
string.
• But it can implented on any kind of platform: PC’s, phones,
tablets, etc..
UCN / IT - 2012
19
SOAP, WSDL and UDDI
• SOAP - Simple Object Access Protocol
– Used when the webservice is called
• WSDL - Web Service Definition Language
– Metadata (description) for the webservice
• UDDI - Universal Description, Discovery and Integration
– Used for registration and searching for webservices
(Is not widely used, use google or xmethods.net instead)
UCN / IT - 2012
20
WSDL
• We saw that VisualStudio could generate proxy code
automatically.
• It is using the WSDL file.
• WSDL = Web Service Description Language
• A formal, platform-neutral definition of a web service
– Provided by a web service as a WSDL document
– Used by clients to obtain information about a web service
Service.wsdl
Service
Page
(stub)
UCN / IT - 2012
Web server
obj
21
Example
• Get Windows Live’s WSDL dokumentation for the search
web service
– http://soap.search.msn.com/webservices.asmx?wsdl.
UCN / IT - 2012
22
The strength of formal techniques and
standardlisation
• Client-side tools can automatically handle WSDL!
• Example:
– make a “Web Reference” in Visual Studio .NET receive the WSDL
and enable IntelliSense, type-check, & proxy generation
//Create an instance of the webservice
RoutePlanner.Service1 route = new RoutePlanner.Service1();
static List<string> _waypoints = new List<string>(); //Stores the
string _country = "Denmark"; //Set country
//Lookup for the address in the service
string[] fr = route.FindAdressOrLocation(textBox1.Text, _country);
UCN / IT - 2012
23
Beware of the architecture
• Data-only marshalling!
• Don't be mistaken:
– It looks like objects is MBV (marshal by value)
google = new GoogleSearchService();
result = google.doGoogleSearch("4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H",
txtSearch.Text, 0, 10, false, "", false, "", "", "");
foreach (ResultElement re in result.resultElements)
...;
– That is not true!
– No code is marshalled, only public data fields
– Web service objects are really MBDO (marshal by data only)
UCN / IT - 2012
24
SOAP
• SOAP - Simple Object Access Protocol
– Used for request and response when the application is runnning.
– Contains information about the method, that is called
– Parameters for the method
– And return values from the method.
UCN / IT - 2012
25
SOAP request
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
:q0="http://tempuri.org/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"
:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<q0:BuyDKK>
<q0:cur>eur</q0:cur>
</q0:BuyDKK>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
UCN / IT - 2012
26
SOAP Response
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
:xsd="http://www.w3.org/2001/XMLSchema"
:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<BuyDKKResponse xmlns="http://tempuri.org/">
<BuyDKKResult>0.13437067494390023</BuyDKKResult>
</BuyDKKResponse>
</soap:Body>
</soap:Envelope>
UCN / IT - 2012
27
WSDL
• WSDL - Web Service Definition Language
Metadata (description) of the webservice
– Can be used by developement tools for generation of proxy
(stub/skeleton)
– Name of the WebService
– Parameters – number, type and order
– Return type
– How to interact with the Web Service using HTTP/SOAP
UCN / IT - 2012
28
Make a web service yourself
• Live demo
• But in practice, some knowledge of XML is needed
UCN / IT - 2012
29
Exercise:
• Make and deploy a webservice, that returns the server time.
• There is a weather web service here:
http://balder.ucn.dk/weather/Weather.asmx
– Find the weather in Aalborg.
• First use SearchLocation to find the location id.
It returns an array of results. Use the id property
• Then get a WeatherData object by GetWeather
– The service get data from weatherbug.com
– You can only use service on Balder for educational use.
– The real service on WeatherBug offers more operations, multiple
protocols, eg. Rest and soap
• Homework:
– Make one of the “follow-me”- exercises in the folder RouteExercise.
The Windows version is a bit easier than the web version.
UCN / IT - 2012
30