09 web services

17
9. Web Services

Upload: anubha-agarwal

Post on 10-Jul-2016

4 views

Category:

Documents


0 download

DESCRIPTION

WebService introduction

TRANSCRIPT

Page 1: 09 Web Services

9. Web Services

Page 2: 09 Web Services

2Microsoft

Objectives

“Web Services are poised to change the future of software development...”

• WebServices

Page 3: 09 Web Services

3Microsoft

Web services?

“Web services are web apps that return data, not presentation. Since applications are typically about accessing data, web services are poised to become the next evolutionary step in distributed software development...”

• Why?– cross-platform application development– legacy system integration

obj obj

objWeb server

objXML

Page 4: 09 Web Services

4Microsoft

Overview

• Web services involve many technologies:– WSDL to learn about web service– to call: proxy objects, SOAP, XML, HTTP and .ASMX pages

obj

obj

Web server

objclient app

proxy

method call

HTTP request

SOAP msg (XML)

.asmx

web service

method call

WSDL

Page 5: 09 Web Services

5Microsoft

Example

• Google

• A great search engine– www.google.com– but what if I want my own GUI?

Page 6: 09 Web Services

6Microsoft

Google web service

• Google offers a web service that performs searches for you• Why?

– clients can build custom GUIs– google.com can make money!

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

// display resulting URLs...foreach (ResultElement re in result.resultElements) lstURLs.Items.Add(re.URL);

Page 7: 09 Web Services

7Microsoft

Working with web services

• Two steps:1. build a web service2. build clients to use it

Page 8: 09 Web Services

8Microsoft

(1) Building a web service

• Start by creating a project of type “ASP.NET Web Service”

Page 9: 09 Web Services

9Microsoft

A web service is…

• One or more objects that respond to web-based method calls– there is no GUI design to a web service– only raw classes with methods…

public class Service1 : System.Web.Services.WebService{ . . .}

Page 10: 09 Web Services

10Microsoft

Example

• Looks like C#, but keep in mind these are web-based methods– client could be calling from any platform– parameters passed using XML

public class Service1 : System.Web.Services.WebService{ [WebMethod] public int Add(int x, int y) { return x + y; }

[WebMethod] public string[] Attendees() { <<open DB, read attendees into array, return it>> }}

attribute

inherit

Page 11: 09 Web Services

11Microsoft

(2) Building a client

• Start by creating a client…– WinForm, WebForm, console-based, anything you want!– for fun, let's use VB…

Page 12: 09 Web Services

12Microsoft

Reference the component

• As usual, we need to reference component– this will activate IntelliSense– this will make sure we call it correctly– this will enable underlying XML +

SOAP communication

• How?– project references, right-click, Add web reference…– type URL for web service, e.g.

• http://localhost/WebService/Service1.asmx

service name

classname

web server

Page 13: 09 Web Services

13Microsoft

Program against component

• Treat web service like any other class!– use new to create instances– make method calls– pass parameters

Private Sub Button1_Click(...) Handles Button1.Click Dim i, j, k As Integer

i = CInt(TextBox1.Text) j = CInt(TextBox2.Text)

Dim obj As localhost.Service1 obj = New localhost.Service1() k = obj.Add(i, j)

MessageBox.Show("Sum = " + k.ToString())End Sub

Page 14: 09 Web Services

14Microsoft

Underlying execution…

• Here's what the call to Add() actually looks like:

Web server

objclient app

proxy

obj.Add(i, j);

HTTP request: Service1.asmx

<Add> <n1>10</n1> <n2>20</n2></Add>

.asmx

web service

obj.Add(10, 20);

Page 15: 09 Web Services

15Microsoft

Summary

• Pretty powerful stuff!

• Lots of technology be used underneath:– XML for parameter-passing– SOAP as protocol– HTTP– ASP.NET – IIS

Page 16: 09 Web Services

16Microsoft

References

• Books:– Y. Shohoud, "Real World XML Web Services: for .NET and VB

.NET Developers"

• Web sites:– http://msdn.microsoft.com/webservices

Page 17: 09 Web Services

17Microsoft

Lab?

• Work on lab #6, "Web Services"…