How to return a more detailed response in your WSDL for web methods with a return type of XmlDocument

Thursday, September 18, 2008 7:50
Posted in category HowTo
you’ve develop your .net web service … get everything working just the way you like … and then you notice that auto generated wsdl provides a less then ideal definition for the response … something like:
 
<s:complexType mixed="true">
    <s:sequence>
        <s:any />
    </s:sequence>
</s:complexType>
 
if instead of providing a separate xsd for the response, you would prefer to embed that schema in
the wsdl ... you need to modify your webservice from:
 
   1:  <WebService(Name:="$WEB_SERVICE_NAME$", Description:="$WEB_SERVICE_DESCRIPTION$" _
   2:  , Namespace:="$WEB_SERVICE_NAME_SPACE$")> _
   3:  <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
   4:  <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
   5:  Public Class $WEB_SERVICE_NAME$
   6:      Inherits System.Web.Services.WebService
   7:   
   8:      <WebMethod(MessageName:="$WEB_METHOD_NAME$", Description:="$WEB_METHOD_DESCRIPTION$")> _
   9:      Public Function $WEB_METHOD_NAME$($WEB_METHOD_PARAMETERS$) As XmlDocument
  10:          'TODO: Add logic to return an XmlDocument with the desired response
  11:      End Function
  12:   
  13:  End Class
 
to:
 
   1:  <WebService(Name:="$WEB_SERVICE_NAME$", Description:="$WEB_SERVICE_DESCRIPTION$" _
   2:  , Namespace:="$WEB_SERVICE_NAME_SPACE$")> _
   3:  <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
   4:  <Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
   5:  Public Class $WEB_SERVICE_NAME$
   6:      Inherits System.Web.Services.WebService
   7:   
   8:      Private Const METHOD_NAME As String = "$WEB_METHOD_NAME$"
   9:      Private Const NAME_SPACE As String = "$WEB_SERVICE_NAME_SPACE$"
  10:   
  11:      <WebMethod(MessageName:="$WEB_METHOD_NAME$", Description:="$WEB_METHOD_DESCRIPTION$")> _
  12:      Public Function $WEB_METHOD_NAME$($WEB_METHOD_PARAMETERS$) As _$WEB_METHOD_NAME$
  13:          Return New _$WEB_METHOD_NAME$($WEB_METHOD_PARAMETERS$)
  14:      End Function
  15:   
  16:      <XmlSchemaProvider("$SCHEMA_METHOD_NAME$")> _
  17:      <XmlRoot(METHOD_NAME, Namespace:=NAME_SPACE)> _
  18:      Public Class _$WEB_METHOD_NAME$
  19:          Implements IXmlSerializable
  20:   
  21:          Private Const METHOD_NAME As String = "$WEB_METHOD_NAME$"
  22:          Private Const NAME_SPACE As String = "$WEB_SERVICE_NAME_SPACE$"
  23:          Private Const SCHEMA_PATH As String = "$SCHEMA_VIRTUAL_PATH$"
  24:   
  25:          Public Sub New($WEB_METHOD_PARAMETERS$)
  26:          End Sub
  27:   
  28:          Public Sub ReadXml(ByVal reader As System.Xml.XmlReader) _
  29:              Implements System.Xml.Serialization.IXmlSerializable.ReadXml
  30:              Throw New System.NotImplementedException()
  31:          End Sub
  32:   
  33:          Public Sub WriteXml(ByVal writer As System.Xml.XmlWriter) _
  34:              Implements System.Xml.Serialization.IXmlSerializable.WriteXml
  35:              'TODO: Add logic to append "writer" with the desired response
  36:          End Sub
  37:   
  38:          Public Function GetSchema() As System.Xml.Schema.XmlSchema _
  39:              Implements System.Xml.Serialization.IXmlSerializable.GetSchema
  40:              Throw New System.NotImplementedException()
  41:          End Function
  42:   
  43:      Public Shared Function $SCHEMA_METHOD_NAME$(ByVal varXmlSchemaSet As XmlSchemaSet) _
  44:          As XmlQualifiedName
  45:          ' Get the path to the schema file.
  46:          Dim m_str_schema_path As String = HttpContext.Current.Server.MapPath(SCHEMA_PATH)
  47:          ' Retrieve the schema from the file.
  48:          Dim m_obj_XmlSerializer As XmlSerializer = New XmlSerializer(GetType(XmlSchema))
  49:          Dim m_obj_XmlTextReader As XmlTextReader = New XmlTextReader(m_str_schema_path)
  50:          Dim m_obj_XmlSchema As XmlSchema = CType( _
  51:              m_obj_XmlSerializer.Deserialize(m_obj_XmlTextReader), XmlSchema)
  52:          varXmlSchemaSet.XmlResolver = New XmlUrlResolver
  53:          varXmlSchemaSet.Add(m_obj_XmlSchema)
  54:          Return New XmlQualifiedName(METHOD_NAME, NAME_SPACE)
  55:      End Function
  56:   
  57:      End Class
  58:   
  59:  End Class
 
happy programming!

You can skip to the end and leave a response. Pinging is currently not allowed.

Leave a Reply