Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "1 Nordjyllands Erhvervakademi - 2009 15. Web Services Purpose Examples of Web Services Architecture Web Services in.Net."— Presentation transcript:

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

2 2 Nordjyllands 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 Y Application X

3 3 Nordjyllands 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

4 4 Nordjyllands 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 DB DTBT Client Server Web server Web Service Web Service Tier ?

5 5 Nordjyllands 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 enginehttp://www.xmethods.net/ *) Google does no longer provide new keys for the soap based web service

6 6 Nordjyllands 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…

7 7 Nordjyllands 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/

8 8 Nordjyllands Erhvervakademi - 2009 Windows Live web service Execute searches & returns results Example from MSDN

9 9 Nordjyllands Erhvervakademi - 2009 A little live demo: Valuta conversion There is a webservice here: –http://www.webservicex.net/CurrencyConvertor.asmxhttp://www.webservicex.net/CurrencyConvertor.asmx

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

11 11 Nordjyllands Erhvervakademi - 2009 Basic architekture Standard RPC, but with use of XML & web server: Client Web server obj (2) XML 119 obj = new WebService(); result = obj.Add(20, 99); (1) XML 20 99 int Add(int x, int y) { return x + y; } Service Page

12 12 Nordjyllands 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) Service Page (stub) obj method call

13 13 Nordjyllands 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 Service Page (stub) Service.wsdl obj

14 14 Nordjyllands Erhvervakademi - 2009 Example Get Windows Live’s WSDL dokumentation for the search web service –http://soap.search.msn.com/webservices.asmx?wsdl.http://soap.search.msn.com/webservices.asmx?wsdl

15 15 Nordjyllands 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);

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

17 17 Nordjyllands 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)

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

19 19 Nordjyllands Erhvervakademi - 2009 SOAP request eur

20 20 Nordjyllands Erhvervakademi - 2009 SOAP Response 0.13437067494390023

21 21 Nordjyllands Erhvervakademi - 2009 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

22 22 Nordjyllands Erhvervakademi - 2009 Make a web service yourself Live demo But in practice, some knowledge of XML is needed

23 23 Nordjyllands Erhvervakademi - 2009 Exercise: Look at the examples in Demos There is a weather web service here: http://www.webservicex.net/globalweather.asmx 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.

24 24 Nordjyllands Erhvervakademi - 2009 Seriliazation

25 25 Nordjyllands Erhvervakademi - 2009 Serialization –Purpose –Standard serializers

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

27 27 Nordjyllands 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

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

29 29 Nordjyllands 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;.... }

30 30 Nordjyllands 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();

31 31 Nordjyllands 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);

32 32 Nordjyllands 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);

33 33 Nordjyllands Erhvervakademi - 2009 Serialization result: Binary

34 34 Nordjyllands Erhvervakademi - 2009 Serialization result: Soap

35 35 Nordjyllands Erhvervakademi - 2009 Serialization result: Xml Something strange here ?

36 36 Nordjyllands 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 id Use fstream.Position { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/10/2696488/slides/slide_36.jpg", "name": "36 Nordjyllands 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.", "description": "–The interface could be: Person GetPerson(int id); –Hints: Remove [NonSerialized] on id Use fstream.Position


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

Similar presentations


Ads by Google