Home   Cover Cover Cover Cover
 

Web service of a bookshop

The web service is implemented in a file with the suffix .asmx. It looks as follows:

/csbook/solutions/19/BookStoreService.asmx
<%@ 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
//------------------------------------------------------------------------------
// 
//     This code was generated by a tool.
//     Runtime Version: 1.0.3705.288
//
//     Changes to this file may cause incorrect behavior and will be lost if 
//     the code is regenerated.
// 
//------------------------------------------------------------------------------

// 
// This source code was auto-generated by wsdl, Version=1.0.3705.288.
// 
using System.Diagnostics;
using System.Xml.Serialization;
using System;
using System.Web.Services.Protocols;
using System.ComponentModel;
using System.Web.Services;


/// 
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Web.Services.WebServiceBindingAttribute(Name="BookStoreServiceSoap", 
  Namespace="http://tempuri.org/")]
public class BookStoreService : System.Web.Services.Protocols.SoapHttpClientProtocol {
    
    /// 
    public BookStoreService() {
        this.Url = "http://dotnet.jku.at/csbook/solutions/19/BookStoreService.asmx";
    }
    
    /// 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Available", 
      RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", 
      Use=System.Web.Services.Description.SoapBindingUse.Literal, 
      ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public bool Available(string isbn) {
        object[] results = this.Invoke("Available", new object[] {
                    isbn});
        return ((bool)(results[0]));
    }
    
    /// 
    public System.IAsyncResult BeginAvailable
    (string isbn, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("Available", new object[] {
                    isbn}, callback, asyncState);
    }
    
    /// 
    public bool EndAvailable(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((bool)(results[0]));
    }
    
    /// 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Author", 
      RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", 
      Use=System.Web.Services.Description.SoapBindingUse.Literal, 
      ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string Author(string isbn) {
        object[] results = this.Invoke("Author", new object[] {
                    isbn});
        return ((string)(results[0]));
    }
    
    /// 
    public System.IAsyncResult BeginAuthor
    (string isbn, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("Author", new object[] {
                    isbn}, callback, asyncState);
    }
    
    /// 
    public string EndAuthor(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }
    
    /// 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Title", 
      RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", 
      Use=System.Web.Services.Description.SoapBindingUse.Literal, 
      ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public string Title(string isbn) {
        object[] results = this.Invoke("Title", new object[] {
                    isbn});
        return ((string)(results[0]));
    }
    
    /// 
    public System.IAsyncResult BeginTitle
    (string isbn, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("Title", new object[] {
                    isbn}, callback, asyncState);
    }
    
    /// 
    public string EndTitle(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((string)(results[0]));
    }
    
    /// 
    [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/Price", 
      RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", 
      Use=System.Web.Services.Description.SoapBindingUse.Literal, 
      ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
    public int Price(string isbn) {
        object[] results = this.Invoke("Price", new object[] {
                    isbn});
        return ((int)(results[0]));
    }
    
    /// 
    public System.IAsyncResult BeginPrice
    (string isbn, System.AsyncCallback callback, object asyncState) {
        return this.BeginInvoke("Price", new object[] {
                    isbn}, callback, asyncState);
    }
    
    /// 
    public int EndPrice(System.IAsyncResult asyncResult) {
        object[] results = this.EndInvoke(asyncResult);
        return ((int)(results[0]));
    }
}

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;

public class BookStoreTest {

  public static void Main() {
    BookStoreService store = new BookStoreService();
    for (;;) {
      Console.Write(">");
      string cmd = Console.ReadLine();
      if (cmd == "") return;
      string isbn;
      if (cmd.StartsWith("price")) {
        isbn = cmd.Substring(6).Trim();
        Console.WriteLine("price = {0}", store.Price(isbn));
      } else if (cmd.StartsWith("author")) {
        isbn = cmd.Substring(7).Trim();
        Console.WriteLine("author = {0}", store.Author(isbn));
      } else if (cmd.StartsWith("title")) {
        isbn = cmd.Substring(6).Trim();
        Console.WriteLine("title = {0}", store.Title(isbn));
      } else if (cmd.StartsWith("avail")) {
        isbn = cmd.Substring(6).Trim();
        if (store.Available(isbn)) Console.WriteLine("available"); 
        else Console.WriteLine("not available");
      }
    }
  }
}

Finally, we compile both files with the following command:

  csc BookStoreService.cs BookStoreTest.cs