30
1 UCN / IT - 2012 Distributed Programming Web Services Purpose Examples of Web Services Architecture Web Services in .Net

Distributed Programming Web Services

Embed Size (px)

DESCRIPTION

Distributed Programming Web Services. Purpose Examples of Web Services Architecture Web Services in .Net. 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 - PowerPoint PPT Presentation

Citation preview

1UCN / IT - 2012

Distributed ProgrammingWeb Services

• Purpose• Examples of Web Services• Architecture• Web Services in .Net

2UCN / IT - 2012

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 YApplication YApplication XApplication X

3UCN / IT - 2012

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

4UCN / IT - 2012

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:

– existing tiers should be used as they are, therefore no recoding

DBDTBT

Client

Server

Web server

WebService

Web Service

Tier

?

5UCN / IT - 2012

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

6UCN / IT - 2012

Amazon.com web service

• Amazon.com offers product information via web service• Why?

– To raise the sale! More than 10% are sold via WS…

7UCN / IT - 2012

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/

8UCN / IT - 2012

Windows Live web service• Execute searches & returns results• Example from MSDN

9UCN / IT - 2012

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.

10UCN / IT - 2012

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

11UCN / IT - 2012

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”

12UCN / IT - 2012

• 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

13UCN / IT - 2012

A little live demo: Valuta conversion

• There is a SOAP webservice here:– http://www.webservicex.net/CurrencyConvertor.asmx

14UCN / IT - 2012

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(); }

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(); }

15UCN / IT - 2012

Basic architekture

• Standard RPC, but with use of XML & web server:

Client

Web server

obj(2) XML

<Add> <result>119</result></Add>

obj = new WebService();result = obj.Add(20, 99);

(1) XML

<Add> <x>20</x> <y>99</y></Add>

int Add(int x, int y){ return x + y; }

ServicePage

16UCN / IT - 2012

More details…

• Proxy and stub objects supports RPC• Messages in SOAP format

– SOAP = Simple Object Access Protocol

Web server

Client

proxy

method call

HTTP request

SOAP msg (XML)

ServicePage(stub)

objmethod call

17UCN / IT - 2012

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#

18UCN / IT - 2012

19UCN / IT - 2012

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..

20UCN / IT - 2012

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)

21UCN / IT - 2012

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

Web server

ServicePage(stub)

Service.wsdl

obj

22UCN / IT - 2012

Example

• Get Windows Live’s WSDL dokumentation for the search web service– http://soap.search.msn.com/webservices.asmx?wsdl.

23UCN / IT - 2012

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 webserviceRoutePlanner.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 servicestring[] fr = route.FindAdressOrLocation(textBox1.Text, _country);

//Create an instance of the webserviceRoutePlanner.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 servicestring[] fr = route.FindAdressOrLocation(textBox1.Text, _country);

24UCN / IT - 2012

Beware of the architecture

• Data-only marshalling!• Don't be mistaken:

– It looks like objects is MBV (marshal by value)

– That is not true! – No code is marshalled, only public data fields– Web service objects are really MBDO (marshal by data only)

google = new GoogleSearchService();result = google.doGoogleSearch("4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H", txtSearch.Text, 0, 10, false, "", false, "", "", "");

foreach (ResultElement re in result.resultElements) ...;

google = new GoogleSearchService();result = google.doGoogleSearch("4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H", txtSearch.Text, 0, 10, false, "", false, "", "", "");

foreach (ResultElement re in result.resultElements) ...;

25UCN / IT - 2012

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.

26UCN / IT - 2012

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>

27UCN / IT - 2012

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>

28UCN / IT - 2012

WSDL

• WSDL - Web Service Definition LanguageMetadata (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

29UCN / IT - 2012

Make a web service yourself

• Live demo

• But in practice, some knowledge of XML is needed

30UCN / IT - 2012

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.