CSCI E-143 CertPrep: Web Services (Kalani, chapter 7)
Saturday, Dec. 18, 2004
This Saturday we cover Kalani's chapter 7, "Web Services". This is a very short chapter. In fact, the part you are actually responsible for
is shorter than the chapter itself,
because Kalani briefly shows how to set up a web service, and for this test, you only have to know how to use a web service,
not how to write one. Nevertheless, some tricky questions might come up.
Terms
A Web Service is a program that exposes is methods for use
over the internet.
It differs from a "web app" because it does not return a web page.
It returns its output in a coded form for use by your Windows client.
You must have a Windows client to pass data to the web service and
to display the output you receive from it.
A Web Method is a routine in a web service that you can invoke across
the internet. (A web service class may of course have its own private
methods not exposed to web access.)
SOAP. Simple Object Access Protocol.
Encapsulates object calls as XML.
Used in passing data both ways between a Windows client and the web service it uses.
Discovery is finding out about web services: where they are,
the names of the classes, and the names and natures of the
web methods they expose.
Disco. A command-line web services discovery tool
used by Microsoft.
- It involves the use of registries, which compile information
about available web services on the 'net.
- It can be used to form proxies for an individual web service,
by reading that service's .asmx
or .wsdl files.
(Disco can't use the
.asmx file directly;
it uses it to try to find the .wsdl file.)
- In addition, .disco is the file extension
for a file containing information retrieved by this program,
including URLs, such as that for the
.wsdl file.
(Example: Kalani, p. 583)
UDDI. Universal Description, Discovery, and
Integration. Another web services tool, also
involving a registry.
But there is no .uddi file extension.
WSDL. Web Services Description Language.
Tells the SOAP message types possible for this service.
In addition,
.wsdl is a file extension for a file containing such information, which might be produced by the various tools for discovering
web services.
Creating a Web Service
You will not be asked about creating a web service on this exam.
(It is covered on exam 70-320.)
But it may clarify things if we do a simple example.
(Kalani, pp. 575-576)
Instantiating and Invoking Web Services from a Windows Application
Some of this might be on the exam.
There are two ways to do this: the hard way and the easy way.
The Hard Way: Using disco.exe and
wsdl.exe
Some command-line options for wsdl.exe
- /language. It's
CS for C#.
- /namespace.
The output namespace. You must run this
wsdl.exe routine if you want to set a
namespace other than the default that would be set by using a web reference.
- /out:[filename]. The output file name.
The Easy Way: Using Web References
(Kalani, pp. 572-573)
- In the Solution Explorer panel, right-click
"References" and select "Add Web Reference".
- You get a dialog box.
Give the URL in the Address bar and press Enter.
Information should be downloaded from the site.
- Click the "Add Reference" button.
This creates the same files created by the command-line options
described just above.
- Finish your project, creating objects from the web service
as if the service was on your local machine.
- You must use a fully qualified name for a class you instantiate from
the web servicefor example,
localhost.Class.
- You do not have make any changes to the names of methods in such a class.
- Right-click on the client project in the Solution Explorer,
and set it as the start-up project.
- Run the (client) project.
There is another advantage to doing it this easy way:
if the web service changes, you can get updated information by
right-clicking the Web reference node in Solution Explorer and selecting
"Update Web Reference".
Asynchronous Web Service Calls
(Kalani, pp. 591-592)
Kalani gives this useful example at the end of the chapter.
It uses a callback method which is invoked when the called web service returns
with the information for the local program to display.
The important thing about this code is that it makes no reference to the web
service that is not already made for a synchronous method.
That is, such asynchronous calls require no coding on the server end.
Here is the code:
(Kalani, p. 593)
private void btnGetWeather_Click(object sender, System.EventArgs e)
{
// Declare the Web service main object
com.capescience.live.AirportWeather aw =
new com.capescience.live.AirportWeather();
// Invoke the Web service. This may take some time, so call it asynchronously.
// First, create a callback method.
AsyncCallback wcb = new AsyncCallback(WebServiceCallback);
// And then initiate the asynchronous call.
// "BegingetSummary" is in the proxy class.
aw.BegingetSummary(txtCode.Text, wcb, aw);
}
// This method will get called when the Web service call is done.
public void WebServiceCallback(IAsyncResult ar)
{
// Retrieve the state of the proxy object.
com.capescience.live.AirportWeather aw =
(com.capescience.live.AirportWeather)ar.AsyncState;
// Call the End method to finish processing.
// "EndgetSummary" is in the proxy class.
com.capescience.live.WeatherSummary ws = aw.EndgetSummary(ar);
// Display the results.
lbResults.Items.Clear();
lbResults.Items.Add(ws.location);
lbResults.Items.Add(“Wind “ + ws.wind); //etc., etc., etc.
}
Here are the relevant questions from the two tests at the end
of the "short Kalani" book:
Test 1: questions
36 (page 373; answer: page 395), 37.
Test 2: questions
23 (page 415; answer: page 443),
24, 25.
(Some of these questions may duplicate those in long Kalani.
Both texts are listed
here.)
Last revised Dec. 17, 2004