300 likes | 420 Views
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
E N D
Distributed ProgrammingWeb 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 • A Windows server gets data from a Linux server Application X Application Y
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
DB Web server Web Service DT BT 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 ? Client Server Web Service Tier
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
Amazon.com web service • Amazon.com offers product information via web service • Why? • To raise the sale! More than 10% are sold via WS…
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/
Windows Live web service • Execute searches & returns results • Example from MSDN
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.
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
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”
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
A little live demo: Valuta conversion • There is a SOAP webservice here: • http://www.webservicex.net/CurrencyConvertor.asmx
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(); }
<Add> <x>20</x> <y>99</y> </Add> (1) XML (2) XML int Add(int x, int y) { return x + y; } <Add> <result>119</result> </Add> obj Basic architekture • Standard RPC, but with use of XML & web server: obj = new WebService(); result = obj.Add(20, 99); Web server Client Service Page
obj More details… • Proxy and stub objects supports RPC • Messages in SOAP format • SOAP = Simple Object Access Protocol Web server Client Service Page (stub) method call method call proxy SOAP msg (XML) HTTP request
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#
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..
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)
obj 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 Web server Service Page (stub)
Example • Get Windows Live’s WSDL dokumentation for the search web service • http://soap.search.msn.com/webservices.asmx?wsdl.
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(); staticList<string> _waypoints = newList<string>(); //Stores the string _country = "Denmark"; //Set country //Lookup for the address in the service string[] fr = route.FindAdressOrLocation(textBox1.Text, _country);
google = new GoogleSearchService(); result = google.doGoogleSearch("4a8/TvZQFHID0WIWnL1CMmMx0sNqhG8H", txtSearch.Text, 0, 10, false, "", false, "", "", ""); foreach (ResultElementre in result.resultElements) ...; 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)
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.
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>
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>
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
Make a web service yourself • Live demo • But in practice, some knowledge of XML is needed
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.