Top Banner
1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in .Net
36

1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

Apr 01, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

1Nordjyllands Erhvervakademi - 2009

15. Web Services

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

Page 2: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

2Nordjyllands Erhvervakademi - 2009

Why web services?

• Applications operates over different platforms– A Java app gets data from a .NET app– A Windows server gets data from a IBM mainframe– A Linux server gets data from a Windows server

Application YApplication YApplication XApplication X

Page 3: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

3Nordjyllands Erhvervakademi - 2009

When to use web services?

• When interoperability between platforms is the important demand. Else:

– remoting is more efficient

– remoting offers more features

– remoting 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

Page 4: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

4Nordjyllands Erhvervakademi - 2009

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

?

Page 5: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

5Nordjyllands Erhvervakademi - 2009

Examples of web services

• What is possible today?• Theres is many different public services:

– Amazon.com client

– TerraServer satellite pictures

– Google* Windows Live search

– MapPoint maps & route planner

– .....

Search for webservices here: http://www.xmethods.net/Or with your favorite search engine

*) Google does no longer provide new keys for the soap based web service

Page 6: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

6Nordjyllands Erhvervakademi - 2009

Amazon.com web service

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

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

Page 7: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

7Nordjyllands Erhvervakademi - 2009

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/

Page 8: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

8Nordjyllands Erhvervakademi - 2009

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

Page 9: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

9Nordjyllands Erhvervakademi - 2009

A little live demo: Valuta conversion

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

Page 10: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

10Nordjyllands Erhvervakademi - 2009

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

Page 11: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

11Nordjyllands Erhvervakademi - 2009

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

Page 12: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

12Nordjyllands Erhvervakademi - 2009

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

Page 13: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

13Nordjyllands Erhvervakademi - 2009

WSDL

• 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

Page 14: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

14Nordjyllands Erhvervakademi - 2009

Example

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

Page 15: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

15Nordjyllands Erhvervakademi - 2009

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

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

foreach (ResultElement re in result.resultElements) lstURLs.Items.Add(re.URL);

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

foreach (ResultElement re in result.resultElements) lstURLs.Items.Add(re.URL);

Page 16: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

16Nordjyllands Erhvervakademi - 2009

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

Page 17: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

17Nordjyllands Erhvervakademi - 2009

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)

Page 18: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

18Nordjyllands Erhvervakademi - 2009

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.

Page 19: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

19Nordjyllands Erhvervakademi - 2009

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>

Page 20: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

20Nordjyllands Erhvervakademi - 2009

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>

Page 21: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

21Nordjyllands Erhvervakademi - 2009

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

Page 22: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

22Nordjyllands Erhvervakademi - 2009

Make a web service yourself

• Live demo

• But in practice, some knowledge of XML is needed

Page 23: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

23Nordjyllands Erhvervakademi - 2009

Exercise:• Look at the examples in Demos• There is a weather web service here:

http://www.webservicex.net/globalweather.asmx– Find the weather in Aalborg.– Make an applikation that shows the weather for towns in your home

country.– You will have to scan some xml. You can do brute force with

String.IndexOf

• Make and deploy a webservice, that returns the server time.

Page 24: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

24Nordjyllands Erhvervakademi - 2009

Seriliazation

Page 25: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

25Nordjyllands Erhvervakademi - 2009

Serialization

• Serialization– Purpose– Standard serializers

Page 26: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

26Nordjyllands Erhvervakademi - 2009

Stream

• A stream is an abstraction for data flowing between a source and a destination

• Stream provides a common way to transfer a sequence of data (e.g. an array) regardless of the device

• The device could be a file, the keyboard, a network connection, a printer, the memory etc.

Page 27: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

27Nordjyllands Erhvervakademi - 2009

Serialization – Send objects by a stream

• A object have to be serialized before it can be send by a stream

• In C#, it is done simply by setting the attribute [Serializable] before the class token.

• 3 methods to serialize:– BinaryFormatter– SoapFormatter– XmlSerializer

Page 28: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

28Nordjyllands Erhvervakademi - 2009

Serilization - continued....

• What is serialized?– By BinaryFormatter is public/private fields and properties

serialized. A remake of the object shall be possible in another place

– By SoapFormatter and XmlFormatter is only public fields and properties serialized.

• If [NonSerialized] is stated before a field/property, then it will not be serialized.

• Note that methods are never serialized.

Page 29: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

29Nordjyllands Erhvervakademi - 2009

Example

• Please note that field should be private, and assigned by properties (but in this way, they can be within the slide ;-))

[Serializable] class Person {

public String FirstName; public String LastName; public DateTime Birthday; public float Height;

[NonSerialized] public int Id;.... }

Page 30: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

30Nordjyllands Erhvervakademi - 2009

Serialize to binary format

using System.IO;using System.Runtime.Serialization.Formatters.Binary;...Person p = new Person(23, "Donald", "Duck", DateTime.Now, 0.4f);Stream bs = new FileStream(@"c:\temp\bp.dat",FileMode.OpenOrCreate,FileAccess.ReadWrite,FileShare.None); BinaryFormatter bf = new BinaryFormatter();bf.Serialize(bs, p);bs.Close();

Page 31: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

31Nordjyllands Erhvervakademi - 2009

Deserialize

using System.IO;using System.Runtime.Serialization.Formatters.Binary;....Stream fstream = File.OpenRead (@"c:\temp\bp.dat”)Person bp = (Person)bf.Deserialize(fstream);Console.WriteLine("{0} {1}", bp.FirstName, bp.BirthDay);

Page 32: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

32Nordjyllands Erhvervakademi - 2009

Send by the socket

string server = "localhost"; int serverPort = 5432; client = new Socket(AddressFamily.InterNetwork,

SocketType.Stream, ProtocolType.Tcp); IPEndPoint serverEndPoint = new IPEndPoint(

Dns.Resolve(server).AddressList[0], serverPort); client.Connect(serverEndPoint); Stream netStream = new NetworkStream(client); Stream bufStream = new BufferedStream(netStream, BUFSIZE);

Page 33: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

33Nordjyllands Erhvervakademi - 2009

Serialization result: Binary

Page 34: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

34Nordjyllands Erhvervakademi - 2009

Serialization result: Soap

Page 35: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

35Nordjyllands Erhvervakademi - 2009

Serialization result: Xml

Something strange here ?

Page 36: 1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net.

36Nordjyllands Erhvervakademi - 2009

Exercise

• Exercise 1– Construct a list of person objects

You are free to use the Person class from the slides– Serialize the objects and save the objects to a binary file– Read from the file and reconstruct the list

• Exercise 2– Make a web service that searches the file for a person with a

specified id and returns it to the client.– The interface could be:

Person GetPerson(int id);– Hints:

Remove [NonSerialized] on idUse fstream.Position<fstream.Length to determine end_of_file