Developing ASMX Web Services and Controlling the Generated WSDL

Microsoft has made the development of web services very simple for developers who use Visual Studio. Add a web service project and the basic plumbing code is generated!
Many developers do not know what goes behind the hood or how to customise the generated WSDL when required.

I’ve listed some salient ones that may be of help…

Apply
[WebService(Namespace = "http://www.johannes.org/webservice/2011/11/03/",Name="ExtendingWebService", Description="version 1.0")]

to the class to get these:
<wsdl:definitions targetNamespace="http://www.johannes.org/webservice/2011/11/03/">
<wsdl:documentation>version 1.0</wsdl:documentation>
...
<wsdl:service name="ExtendingWebService">


Apply
[WebMethod(Description="This web service is a typical one")]

to the operation to get this:
<wsdl:operation name="...">
<wsdl:documentation>This web service is a typical one</wsdl:documentation>
...
</wsdl:operation>


Apply
[return: System.Xml.Serialization.XmlElementAttribute("ReturnedStatus")]

to the returned type from the operation to get this:
<s:element name="...">
<s:complexType>
<s:sequence>
<s:element name="ReturnedStatus" type="..."/>
</s:sequence>
</s:complexType>
</s:element>


Add the following
[XmlAnyElement()] System.Xml.XmlElement[] Any,
[XmlAnyAttribute()] System.Xml.XmlAttribute[] AnyAttr

to the operation parameter to get this:
<s:any maxOccurs="unbounded" minOccurs="0"/>
...
<s:anyAttribute/>


Apply
[XmlElement("MyElementName")]

to the parameter/ property to get this:
<s:element name="MyElementName" type="..."/>


Apply
[XmlArray("MyChildren")]
[XmlArrayItem("Child")]

to a parameter/ property of array type to get this:
<s:element name="MyChildren" type="tns:ArrayOfMyClass"/>
...
<s:complexType name="ArrayOfMyClass">
<s:sequence>
<s:element name="Child" nillable="true" type="tns:MyClass"/>
</s:sequence>
</s:complexType>


Apply
[System.ComponentModel.DefaultValue(-1)]

to a parameter/ property to get this:
<s:element ... minOccurs="0" default="-1"/>


Apply
[XmlElementAttribute(DataType = "date")]

to a DateTime parameter/ property to get this:
<s:element ... type="s:date" maxOccurs="1" minOccurs="1"/>


Other points of interest are:
  1. Enum type will result in enumeration in XML
  2. Custom classes will result in complex types in XML
  3. The input parameters to an operation will aggregate into a complex type in XML
  4. Value types (e.g. int, double, DateTime) will always result in minOccurs=”1” and maxOccurs=”1” for that element
  5. Reference types (e.g. string) will always result in minOccurs=”0” and maxOccurs=”1” for that element
  6. Nillable types (e.g. int?, DateTime?) will result in nillable=”true” for that element
  7. Setting XmlElement(IsNullable=true) will result in minOccurs=”1” regardless of whether the applied type is value or reference

Comments

Popular posts from this blog

Understanding ITIL Service Management the UML way…

How to depict (Professional-Looking) Logical Network Diagrams in Astah