Strange Bug in ASP.NET Sample SOAP request/response generation

I encountered a strange bug in how ASP.NET generates sample SOAP
request/responses in .NET 1.1 (this may be the case on 1.0 as well – haven’t
tested). Take a look at this very simple web service, that includes a header:

using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Serialization;
namespace TestHeaderWS {
    public class MyHeaderType: SoapHeader {
        public System.Xml.XmlElement AnyThing;
    }
    public class TestHeaderService: System.Web.Services.WebService { [WebMethod][SoapHeader("MyHeader")][
        return: XmlElementAttribute(Namespace = "")] public string DoSomething() {
            return null;
        }
        public MyHeaderType MyHeader;
    }
}

Now point your browser to http://localhost/TestHeaderWS/TestHeaderService.asm
x
, select the
DoSomething operation, and take a look at the sample SOAP request displayed.
You should see something like this:

<?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:Header>
        <MyHeaderType xmlns="http://tempuri.org/">
            <AnyThing>
                <DoSomethingResult xmlns="">
                    <string>string</string>
                    <string>string</string>
                </DoSomethingResult>
                <string xmlns="">string</string>
            </AnyThing>
        </MyHeaderType>
    </soap:Header>
    <soap:Body>
        <DoSomething xmlns="http://tempuri.org/"/>
    </soap:Body>
</soap:Envelope>

Notice how messed up the sample response is – for some reason, it is embedding
what actually is the result XML (DoSomethingResult) in the header. There are
two triggers to cause this bug (fyi, the service appears to actually work
properly, so this isn’t a big deal).

  1. There needs to be a header with generic XmlElement member.
  2. There needs to be a return: attribute on the operation that has an empty (“”) namespace qualifier. Change this namespace to “foo”, and you’ll see the proper sample response. Strange.