Title: Creating and Consuming a Web Service
1Creating and Consuming aWeb Service
MIS426 ASP.NET M. Featherman Washington State
University
http//dotnet.cbe.wsu.edu/featherman/ MoneyConver
ter/WebForm1.aspx
2Whats the big deal about Web Services??
- They allow developers to share their work, and
bring together disparate info systems (regardless
of development language, and machine host type) - Any old code can be rolled into a web-service and
consumed by others - Allow code to be consumed with or without user
intervention (machine can call code in effect
ask for help/outsource - on its own) - A way around the proprietary mess as they use XML
(to format the requests and responses) and HTTP - .NET makes this all incredibly easy
3An Example - Overview
- A webservice .asmx file has been created and sits
on the edu.wsu.cbe.dotnet server - It converts between dollars and euros
- It can be referenced/called by any .aspx page
- The client .aspx page
- Works with a proxy (referenced copy) of the
webservice - Feeds the webservice values that are used as
function arguments - Instantiates the web service and makes the
function calls and - displays the results
4Overview
- In effect we are calling a function in a class
that someone else wrote - The code exists on someone elses server
- We can consume their code to get a project
completed more quickly - We dont have to know how to perform the
calculation, just what arguments to feed it and
how to handle the return value - Web services are often provided for a fee
5Agenda
- We will now look at the web service's code
- Create a new .aspx project
- Add a web reference to the .asmx (web service)
file - Use the VS IDE to prep the UI
- Write the code in the aspx file
- Run the project
- Look at the glossary of terms
6Converter.asmx has 3 functions in a class called
Converter
- Function EuroToDollar this accepts a euro
amount and returns the equivalent dollars they
are worth - Function DollarToEuro - this accepts a dollar
amount and returns the equivalent euros they are
worth - Private Function GetEuroToDollarConversionRate
an internal function that mimics a table lookup
to provide the up-to-date conversion rate
7A Web Service has been Createdhttp//dotnet.cbe.w
su.edu/featherman/Converter/Converter.asmx
- ltWeb Service(Description"A web service for
converting currencies", _ - Namespace"http//tempuri.org/")gt _
- Public Class Converter this class will be
consumed in the .aspx file - Inherits System.Web.Services.WebService
- ltWebMethod(Description"Convert from Euro to US
Dollar currency")gt _ - Function EuroToDollar(ByVal amount As
Decimal) As Decimal - Return amount GetEuroToDollarConversionR
ate() - End Function
8Converter.asmx (Cont)
- ltWebMethod(Description"Convert from US Dollar
to Euro currency")gt _ - Function DollarToEuro(ByVal amount As
Decimal) As Decimal - Return amount / GetEuroToDollarConversionR
ate() - End Function
- Private Function GetEuroToDollarConversionRate
() As Decimal - 'a real app would read this from an
array or database - Return 0.9
- End Function
9Now make the .aspx page that references and
utilizes it
10Add the WebReference
- In the solution explorer, right-click references
and choose add web reference then paste in the
URL - http//dotnet.cbe.wsu.edu/featherman/Converter/Co
nverter.asmx - Click OK and a reference to the webservice will
be added
This is where the WebService lives
Heres its code
11Write .ASPX code that uses the web-service class
- Private Sub btnToEuros_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnToEuros.Click - 'create the proxy
- Dim conv As New edu.wsu.cbe.dotnet.Convert
er() - 'get the argument that will be passed
into the method of the webservice (amount) - Dim decValue As Decimal
CDec(txtDollars.Text) - 'call the remote method of the webservice
- Dim decResult As Decimal
conv.DollarToEuro(decValue) - 'display the result of the calculation
- Me.txtEuros.Text FormatNumber(decResult.
ToString, 2) - End Sub
12Write .ASPX code that uses the web-service class
- Private Sub btnToDollars_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Handles btnToDollars.Click - Dim conv As New edu.wsu.cbe.dotnet.Converter()
- Dim decValue As Decimal CDec(txtEuros.Text)
- Dim decResult As Decimal conv.EuroToDollar(decV
alue) - Me.txtDollars.Text FormatCurrency(decResult.ToS
tring, 2) - End Sub
13Success!!
14Glossary Web-Service
- Discrete units of programmatic functionality
(code w/functions) exposed to clients
(.aspx/.ascx or windows projects) via
standardized communications protocols (HTTP) and
data formats (XML)
15Glossary UDDI Advertising web services
- UDDI Universal Description, Discovery and
Integration a multi-vendor initiative designed
to provide an web-based business registry so that
creators of web services may register their
publicly available web services - a standard way to expose info about (and sell)
available web-services (codified functionality)
16Glossary WSDL - Describing to consumers what
the web service can do
- WSDL Web service description language an
XML-based standard for docs that describe the
services exposed by the web service (assembly
metadata) - Used to describe the publicly available methods,
parameters, datatypes and return value datatype - Every web service registered with a UDDI registry
needs this info called its WSDL contract
17Glossary - SOAP - How do we exchange info between
the web service and client calling it?
- Using a protocol called SOAP Simple object
Access Protocol A lightweight protocol
XML-based messaging protocol for exchange of info
in a decentralized, distributed environment - Provides a framework for formatting the - SOAP
messages (called envelopes) and how to process
them - Has data encoding, and request/response
processing standards