title: Mapping WSDL enumeration to string in ASP.NET webservice
body: <p><strong>Background:</strong>
  I'm in the process of creating a web service using ASP.NET 2.0. This web service provides another interface to an existing web form which contains selection boxes dynamically populated from a database.</p>
  
  <p>My first draft of the web service accepted a string for each of these and then ensured that it was valid, throwing back an error if it wasn't. However the consumer of the web service has asked, since the possible values aren't likely to change all that often, that we provide enumerated values in the WSDL.</p>
  
  <p>I am reluctant to create an enumeration with my web service code, so I have instead altered the generated WSDL file and instructed my web service to use that instead of inspecting my classes to generate it.</p>
  
  <p><strong>WSDL:</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://example.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://example.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"&gt;
    &lt;wsdl:types&gt;
      &lt;s:schema elementFormDefault="qualified" targetNamespace="http://example.org/"&gt;
        &lt;s:element name="MyMethod"&gt;
          &lt;s:complexType&gt;
            &lt;s:sequence&gt;
              &lt;s:element minOccurs="0" maxOccurs="1" name="myClass" type="tns:MyClass" /&gt;
            &lt;/s:sequence&gt;
          &lt;/s:complexType&gt;
        &lt;/s:element&gt;
        &lt;s:complexType name="MyClass"&gt;
          &lt;s:sequence&gt;
            &lt;s:element minOccurs="0" maxOccurs="1" name="MyString" type="tns:MyStringPossibleValues" /&gt;
          &lt;/s:sequence&gt;
        &lt;/s:complexType&gt;
        &lt;s:element name="MyMethodResponse"&gt;
          &lt;s:complexType /&gt;
        &lt;/s:element&gt;
        &lt;s:simpleType name="MyStringPossibleValues"&gt;
          &lt;s:restriction base="s:string"&gt;
            &lt;s:enumeration value="alpha" /&gt;
            &lt;s:enumeration value="bravo" /&gt;
          &lt;/s:restriction&gt;
        &lt;/s:simpleType&gt;
      &lt;/s:schema&gt;
    &lt;/wsdl:types&gt;
    &lt;wsdl:message name="MyMethodSoapIn"&gt;
      &lt;wsdl:part name="parameters" element="tns:MyMethod" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:message name="MyMethodSoapOut"&gt;
      &lt;wsdl:part name="parameters" element="tns:MyMethodResponse" /&gt;
    &lt;/wsdl:message&gt;
    &lt;wsdl:portType name="ExternalAccessSoap"&gt;
      &lt;wsdl:operation name="MyMethod"&gt;
        &lt;wsdl:input message="tns:MyMethodSoapIn" /&gt;
        &lt;wsdl:output message="tns:MyMethodSoapOut" /&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:portType&gt;
    &lt;wsdl:portType name="ExternalAccessHttpGet" /&gt;
    &lt;wsdl:portType name="ExternalAccessHttpPost" /&gt;
    &lt;wsdl:binding name="ExternalAccessSoap" type="tns:ExternalAccessSoap"&gt;
      &lt;soap:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;
      &lt;wsdl:operation name="MyMethod"&gt;
        &lt;soap:operation soapAction="http://example.org/MyMethod" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;soap:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
    &lt;wsdl:binding name="ExternalAccessSoap12" type="tns:ExternalAccessSoap"&gt;
      &lt;soap12:binding transport="http://schemas.xmlsoap.org/soap/http" /&gt;
      &lt;wsdl:operation name="MyMethod"&gt;
        &lt;soap12:operation soapAction="http://example.org/MyMethod" style="document" /&gt;
        &lt;wsdl:input&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:input&gt;
        &lt;wsdl:output&gt;
          &lt;soap12:body use="literal" /&gt;
        &lt;/wsdl:output&gt;
      &lt;/wsdl:operation&gt;
    &lt;/wsdl:binding&gt;
  &lt;/wsdl:definitions&gt;
  </code></pre>
  
  <p><strong>Webservice:</strong></p>
  
  <pre><code>namespace Example.Service
  {
      [WebService(Namespace = "http://example.org/")]
      [WebServiceBinding(
          ConformsTo = WsiProfiles.BasicProfile1_1,
          Location="ExternalAccess.wsdl",
          Name="ExternalAccessSoap",
          Namespace = "http://example.org/")]
      [ToolboxItem(false)]
      public class ExternalAccess : System.Web.Services.WebService
      {
          public class MyClass
          {
              public string MyString;
          }
  
          [WebMethod]
          [SoapDocumentMethod(
              Action = "http://example.org/MyMethod",
              RequestNamespace = "http://example.org/",
              Binding="ExternalAccessSoap")]
          public void MyMethod(MyClass myClass)
          {
          }
      }
  }
  </code></pre>
  
  <p><strong>The problem:</strong>
  As the WSDL specifies an enumeration for MyString and the code specified a string type, ASP.NET does not manage to map the fields correctly.</p>
  
  <p>Is there an attribute I can use to instruct the deserialiser to populate the string field with the value of the enumeration?</p>
  
  <p>Regards,</p>
  
  <p>Matt</p>