ASP.NET Web Services documentation and derived classes

1 minute read

This is another gotcha that I ran into this week. The documentation pages generated by the ASMX engine don’t seem to handle derived/base class types correctly. To illustrate this problem, create a demo .asmx file with the following contents:

<%@ WebService Language="c#" Class="MyService" %>

public class MyBase {
    public int baseVal = 5;
}

public class MyDerived : MyBase {
    public int derivedVal = 10;
}

public class MyService {
    [System.Web.Services.WebMethod]
    public MyDerived MyTest() {
        return new MyDerived();
    }
}

The generated WSDL correctly contains the type definitions for MyBase and MyDerived:

<s:complexType name="MyDerived">
  <s:complexContent mixed="false">
    <s:extension base="s0:MyBase">
      <s:sequence>
        <s:element minOccurs="1" maxOccurs="1" name="derivedVal" type="s:int" /> 
      </s:sequence>
    </s:extension>
  </s:complexContent>
</s:complexType>
<s:complexType name="MyBase">
  <s:sequence>
    <s:element minOccurs="1" maxOccurs="1" name="baseVal" type="s:int" /> 
  </s:sequence>
</s:complexType>

And the implementation of the method returns the correct result:

<?xml version="1.0" encoding="utf-8" ?> 
<MyDerived xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://tempuri.org/">
  <baseVal>5</baseVal> 
  <derivedVal>10</derivedVal> 
</MyDerived>

But the examples in the generated documentation page only show the members from the derived class:

HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <MyTestResponse xmlns="http://tempuri.org/">
      <MyTestResult>
        <derivedVal>int</derivedVal>
      </MyTestResult>
    </MyTestResponse>
  </soap:Body>
</soap:Envelope>

Note that only <derivedVal> is shown in the documentation and <baseVal> is missing. I spent a little while looking at this before I realised that it was just the documentation page and that the schema was correct.

Updated: