Home   Cover Cover Cover Cover
 

Web-Service einer Buchhandlung

Der Web-Service selbst wird in einer Datei mit der Endung .asmx implementiert und sieht so aus:

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;
  }
  
}

Achtung: Die aspx-Datei muss in einem virtuellen Verzeichnis unseres Web-Servers oder in einem Unterverzeichnis davon stehen.

Nun können wir unseren Web-Service bereits testen, indem wir den Browser auf die Adresse
     http://dotnet.jku.at/csbuch/solutions/24/BookStoreService.asmx
richten. Unter
     http://dotnet.jku.at/csbuch/solutions/24/BookStoreService.asmx?WSDL
sehen wir die WSDL-Beschreibung unseres Web-Services.

Um den Web-Service von einem anderen Programm aus benutzen zu können, muss man mit wsdl.exe eine Proxy-Klasse erzeugen:

  wsdl /out:BookStoreService.cs http://dotnet.jku.at/csbuch/solutions/24/BookStoreService.asmx?WSDL

Diese Proxy-Klasse sieht im konkreten Fall so aus:

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]));
    }
}

Nun muß man noch ein Hauptprogramm schreiben, das den Web-Service über die Proxy-Klasse aufruft. Das folgende Programm liest Kommandos z.B. der Art

  price 0201485419
  title 0201485419
  author 0201485419

und ruft die betreffenden Methoden des Web-Service auf:

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");
      }
    }
  }
}

Zum Schluss übersetzen wir beide Programmteile wie folgt:

  csc BookStoreService.cs BookStoreTest.cs