<%@ WebService Language="C#" Class="BookStoreService"%>
using System;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections;
public class Book {
public string author;
public string title;
public int price;
public Book(string a, string t, int p) {
author = a; title = t; price = p;
}
}
public class BookStoreService: WebService {
Hashtable books = new Hashtable();
public BookStoreService() {
books["0201485419"] = new Book("D.Knuth", "The Art of Computer Programming", 45);
books["0201066726"] = new Book("R.Sedgewick", "Algorithms", 32);
books["0201634465"] = new Book("D.Box", "Essential COM", 34);
}
[WebMethod]
public bool Available(string isbn) {
return books[isbn] != null;
}
[WebMethod]
public string Author(string isbn) {
object obj = books[isbn];
if (obj == null) return ""; else return ((Book)obj).author;
}
[WebMethod]
public string Title(string isbn) {
object obj = books[isbn];
if (obj == null) return ""; else return ((Book)obj).title;
}
[WebMethod]
public int Price(string isbn) {
object obj = books[isbn];
if (obj == null) return 0; else return ((Book)obj).price;
}
}
Note: The aspx file must be stored in a virtual directory of the web server
or in one of its subdirectories.
We can now test our web service by pointing the web browser to
http://dotnet.jku.at/csbook/solutions/19/BookStoreService.asmx
With
http://dotnet.jku.at/csbook/solutions/19/BookStoreService.asmx?WSDL
we get a WSDL description of our web service.
In order to make the web service available for other programs we must
generate a proxy class from it using wsdl.exe:
wsdl /out:BookStoreService.cs http://dotnet.jku.at/csbook/solutions/19/BookStoreService.asmx?WSDL
The proxy class of our example looks as follows:
/csbook/solutions/19/BookStoreService.cs
//------------------------------------------------------------------------------ |
Next, we must write a main program that calls the web service via the
proxy class. For example, the following program reads commands of the kind:
price 0201485419
title 0201485419
author 0201485419
and calls the appropriate methods of the web service:
/csbook/solutions/19/BookStoreTest.cs
using System; |
Finally, we compile both files with the following command:
csc BookStoreService.cs BookStoreTest.cs


