Title: Web Services
1Web Services
2Introduction
- Today's Web sites that just deliver user
interface pages to browsers - Next generation is programmable Web sites that
link organizations, applications, services, and
devices with one another. - The common language runtime provides built-in
support for creating and exposing Web Services
(XML Web Services) - Web Services make web a virtual library of
ready-made code.
3Progression
- They are a technology that make interactions of
objects over the internet possible ! - Natural progression
- OO-languages lets objectsinteracts within the
same application - Protocols such as COM let objects on the same
same computer but in different applications
interact. - Protocols such as DCOM let objects on different
computers but the same network, interacts - Web services lets objects on different computers
interact over the internet.
4What is a Web Service ?
- A component of programmable application that can
be accessed using standard web protocol. - Using XML messages transmitted over the HTTP
protocol. - A black box that
- accepts requests from a consumer (an application
or a web application) - performs a specific task
- Returns the result (self describing xml document)
- The user can use it as if it was a local
component - Dont need to know anything about how the
service function - Only the parameters and the result
5Invoke your first web service 1
- Make a new project
- Rigth on the Reference folder in Solution
explorer. Add Web reference. - Type http//live.capescience.com/wsdl/AirportWeath
er.wsdl in the Adress bar - Click add reference
- Make a web form (see next slide)
6Add web reference
7Invoke your first web service 2
8Invoke your first web service 3
- private void Button1_Click(object sender,
System.EventArgs e) -
- com.capescience.live.AirportWeather aw new
com.capescience.live.AirportWeather() - com.capescience.live.WeatherSummary ws
aw.getSummary(TextBox1.Text) - ListBox1.Items.Clear()
- ListBox1.Items.Add(ws.location)
- ListBox1.Items.Add("Wind " ws.wind)
- ListBox1.Items.Add("Sky " ws.sky)
- ListBox1.Items.Add("Temperature " ws.temp)
- ListBox1.Items.Add("Humidity " ws.humidity)
- ListBox1.Items.Add("Barometer " ws.pressure)
- ListBox1.Items.Add("Visibility "
ws.visibility)
9Invoke your first web service 4
10What happens ?
- When you make a web reference Visual Studio reads
the WSDL file - Generate proxy class
- When you call the method from the remote server
.NET translate your call into SOAP and transmit
it to the remote method and receives the result
in SOAP and convert it to .NET datatype - By default the client uses synchronous methods to
communicate with the Web service - The client wait for the result
11Greeting.asmx
using Systemusing System.Collectionsusing
System.ComponentModelusing System.Datausing
System.Diagnosticsusing System.Webusing
System.Web.Services namespace
MiFirstWebService public class Service1
System.Web.Services.WebService public
Service1() InitializeComponent() WebM
ethod public string HelloWorld() return
"Hello World"
12Definition of a Web service
- using Systemusing System.Web.Servicesusing
System.Xml.Serialization - Next, it imports the namespace.
- public class Service1
- Next, the class is declared.
- WebService(Namespace"http//www.rhs.dk/webservic
es/") - Defines a namespace for the class
- Each XML Web service needs a unique namespace in
order for client applications to distinguish it
from other services on the Web. - Allow us to organize the programming components
into categories. - WebMethod
- any methods that will be accessible as part of
the service have the attribute WebMethod. - Web service files saved under the .asmx file
extension. - Like .aspx files, these are automatically
compiled by the ASP.NET runtime when a request to
the service is made (subsequent requests are
serviced by a cached precompiled type object
13Greeting.asmx
14HTTP Request-Response
- Web services rely on the mechanism of HTTP
Request-Response system. - All information we submit to a web service is
sent as a HTTP Request - Contains the data
- HTTP GET http//localhost8083/greeting.asmx/Hel
lo?namepoulh - HTTP POST use the body of the transmission to
carry the name-value pairs to the web service - All Information we get back from the Web Service
is a HTTP Response. - Contains the result data
- Makes it easy to acces from a browser
15HTTP GET
16HTTP POST
17HTTP
- We need to be able to
- pass more than integers and strings.
- Ex. Objects, datasets .
- Web services use XML to describe data sent from
the consumer and returned from the web service.
18SOAP (Simple Object Acces Protocol)
- SOAP is a lightweight protocol for exchange of
information over the internet. - SOAP is not restricted to name/value pairs
- Defined by W3C
- Uses XML syntax to format its content.
- The SOAP request is submitted as part of an HTTP
post request. - Provides an effective way to call all sorts of
methods
19Advantages of using SOAP
- HTTP is pervasive. It can travel to any point on
the internet regardless of hardware or firewall. - Because is SOAP is XML based, it can be
interpreted by a wide variety of software on many
operating systems.
20SOAP example
21SOAP
- The SOAP message consist of an envelope that
contains a body, each of them is identified with
a specific XML tag. - It invokes a method called Hello
- The parameter is called name of type string.
- The response is of type String wrapped as an XML
element.
22Datatypes
- Various data types are supported by SOAP and can
be passed to and returned from Web Service
methods - Primitive
- Enum Types
- Arrays of Primitives, Enums
- Classes and Structs
- DataSet
- ..
23SOAP
- Web Services are based on open protocols such as
the Simple Object Access Protocol (SOAP) - This client technology can also be used to
consume non-ASP.NET Web Services.
24Web Service AttendeeWebservice
- using Systemusing System.Collectionsusing
System.ComponentModelusing System.Datausing
System.Diagnostics - using System.Webusing System.Web.Services using
System.Data.OleDb - namespace AttendeeWebservice
-
- public class Service1 System.Web.Services.WebSe
rvice -
- public Service1()
-
- InitializeComponent()
-
- WebMethod
- public string findAttendee(string fname, string
lname) -
- return getAttendee(fname, lname)
-
-
25Web Service AttendeeWebservice
- private string getAttendee(string firstName,
string lastName) -
- IDbConnection dbConn String sConnection
- string result
- sConnection "ProviderMicrosoft.Jet.OLEDB.4.0"
"Data Sourcec/Attendees.mdb" - dbConn new OleDbConnection(sConnection)
- dbConn.Open()
- string sql "Select From Attendees "
- if (firstName ! "" lastName ! "") sql
" where FirstName '" firstName "' and
LastName '" lastName "'" - IDbCommand dbCmd
- dbCmd new OleDbCommand()
- dbCmd.CommandText sql
- dbCmd.Connection dbConn
- IDataReader dbReader
- dbReader dbCmd.ExecuteReader()
- if (dbReader.Read())
-
- result dbReader"AID".ToString() " "
result dbReader"FirstName".ToString() "
" - result dbReader"LastName".ToString() "
" result dbReader"Email".ToString()
" "
26AttendeeWebservice(continued)
- WebMethod
- public DataSet getAllAttendee()
- return getAll()
-
-
- private DataSet getAll()
-
- IDbConnection dbConn
- String sConnection
- sConnection "ProviderMicrosoft.Jet.OLED
B.4.0" "Data Sourcec/Attendees.mdb" - OleDbConnection con new
OleDbConnection(sConnection) - string sql
- sql "Select From Attendees Order By
LastName Asc, FirstName Asc" - OleDbDataAdapter DataAdapter new
OleDbDataAdapter(sql,con ) - DataSet dataSet new DataSet()
- DataAdapter.Fill(dataSet, "Attendees")
- return dataSet
-
27WSDL (Web Service Description Language)
- An XML file thats define how the interaction
between a Web Service and its consumer will
occur. - A cross-platform standard from W3C.
- Can define interaction independent of platform
(Unix, Mac, Windows..) and programming language
(ASP.NET, java). - Finding a description (WSDL) of a web service
- http//localhost8083/findattendeeservice.asmx?wsd
l - Gives all the information we need to
use/communicate with the web service.
28WSDL files define
- The data types that it can process
- The methods that it exposes
- The URLs through which those methods can be
accessed.
29WSDL
30Consuming a Web Service 1
Consumer
Application
7
1
Proxy
6
2
5
3
Web Service
4
31Consuming a Web Service 2
- The application executes a function in the proxy
code. - Passing parameters
- The proxy receives the call and formulates the
request to the webservice - The function call is sent to the web service.
- Across LAN or Internet.
- The web service uses the parameters to executes
its function, builds XML for the result. - The XML result is sent to the proxy at the
consumer. - The proxy parses the XML result and retrieve the
values. - The application recieves the values from the
proxy function.
32Proxy
- A Proxy is a stand in for te actual code you want
to call. - Managing the call across the network.
- The proxy resides on the consumers machine
- React as an relay between the consumer and the
web service. The web service is mapped in the
proxy. - The WSDL file is use to create the proxy
- WSDL, tells which methods can be called, the
parameters and the result. - The proxy handles all the network related work.
- The consumer dont know anything about sending
data and WSDL. - The consumer use the web service as if it was a
part of the application itself.
33Using findAttendeeservice.aspx
34Add web reference http//localhost/AttendeeWebser
vice/Service1.asmx
35consumeAttendeeWebservice.aspx
- private void Button1_Click(object sender,
System.EventArgs e) -
- localhost.Service1 wsAttendee new
localhost.Service1() - string firstname TextBox1.Text
- string lastname TextBox2.Text
- TextBox3.Text wsAttendee.findAttendee(firstname
, lastname) -
- private void Button2_Click(object sender,
System.EventArgs e) -
- localhost.Service1 wsAttendee new
localhost.Service1() - DataGrid1.DataSource wsAttendee.getAllAttendee(
) - DataGrid1.DataBind()
36Web service discovery
- How to find a web service ?
- Disco
- A Microsoft standard for creating discovery
documents. Not used outside the .NET world - UDDI
- Universal Description, Discovery and Integration
- A method for finding services by referring to a
central directory. Developed by several
industrial partners IBM, Microsoft - See www.uddi.org