Home   Cover Cover Cover Cover
 

Abstract Parameter Types and Inheritance


From Section 7.4.2 of the book

This example shows how to use abstract parameter types and their subclasses. If a subclass of an abstract parameter type is to be used in a web service call this subclass has to be included into the WSDL description of the web service using the attributes [SoapInclude] or [XmlInclude]. In our example we have to include the subclasses Customer and Employee of the abstract base class Person.

[SoapInclude] is needed for calls that use SOAP-HTTP. [XmlInclude] is needed for calls that use HTTP-GET and HTTP-POST.

PersonService.asmx
<%@ WebService Language="C#" Class="PersonService" %>
using System; 
using System.Web.Services; 
using System.Web.Services.Protocols;
using System.Xml.Serialization;

public class PersonService : WebService {
  
  [WebMethod]
  [SoapRpcMethod]
  [SoapInclude(typeof(Customer)),   // possible subclasses of Person 
    SoapInclude(typeof(Employee))]  // (for SOAP-HTTP calls)
  [XmlInclude(typeof(Customer)),    // possible subclasses of Person 
    XmlInclude(typeof(Employee))]   // (for HTTP-GET/POST calls)
  public Person Find(string ssn) {
    Person p = null;
    if (ssn == "1") 
      p = new Customer(ssn, "John Miller", "EMP-33");
    else 
      p = new Employee(ssn, "Susan Howard");
    return p;
  }

  [SoapRpcMethod] [WebMethod]
  public Person[] GetAll() {
    Person[] data = new Person[2];
    data[0] = new Customer("1", "John Miller", "EMP-33");
    data[1] = new Employee("EMP-33", "Susan Howard");
    return data;
  }
}

public abstract class Person {
  public string SSN; public string Name;
  public Person() {}
  public Person(string ssn, string name) { this.SSN = ssn; this.Name = name;}
}

public class Customer : Person {
  public string EmpSSN;
  public Customer() {EmpSSN = "??"; }
  public Customer(string ssn, string n, string e): base(ssn, n) {EmpSSN = e;}
}

public class Employee : Person {
  public Employee() {}
  public Employee(string ssn, string name) : base(ssn, name) {}
}

If you copy this code to a file PersonService.asmx in a virtual directory of your local machine you can open it with Internet Explorer and get a test page that allows you to invoke the methods Find and GetAll.

Alternatively, you can test your web service also with WebService Studio. Compare the results that are returned by WebService Studio and by Internet Explorer.