在.NET Web服务中将soap前缀更改为soapenv

时间:2014-09-16 18:26:45

标签: asp.net vb.net web-services soap axis

我正在开发一个遗留的Web服务,该服务最初是使用Axis在Java中开发的,其响应是:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soapenv:Body>
      <ns1:TransaccionResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://DefaultNamespace">
         <TransaccionReturn xsi:type="xsd:string"><!-- info --></TransaccionReturn>
      </ns1:TransaccionResponse>
   </soapenv:Body>
</soapenv:Envelope>

我正在制作一个应该与所有当前客户兼容的.NET Web服务,但到目前为止我已经:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <ns1:TransaccionResponse xmlns:ns1="http://DefaultNamespace">
         <TransaccionReturn><!-- info --></TransaccionReturn>
      </ns1:TransaccionResponse>
   </soap:Body>
</soap:Envelope>

我从一个旧的ASP.NET Web服务项目开始,我想知道是否有办法将 soap 前缀替换为 soapenv ?还有什么方法可以强制Web服务添加 xsi:type 声明吗?

1 个答案:

答案 0 :(得分:0)

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Web.Services.Description
Imports System.Xml.Serialization
Imports System.IO

<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class ExpedientesService
    Inherits System.Web.Services.WebService

    Public Sub New()
        MyBase.New()
    End Sub

    <WebMethod()> _
    <SoapDocumentMethod("", _
                        RequestNamespace:="http://DefaultNamespace", _
                        ResponseNamespace:="http://DefaultNamespace", _
                        ParameterStyle:=SoapParameterStyle.Bare)> _
    Public Function llamarWS( _
    <XmlElement("Transaccion", Namespace:="http://DefaultNamespace")> ByVal tr As Transaccion) As _
    <XmlElement("TransaccionResponse")>  _
    RespuestaXML
          Return New RespuestaXML(String.format("You sended: '{0}' '{1}' '{2}'", tr.transaccion, tr.usuario, tr.password))
    End Function

End Class

'HERE THERE IS A CLASS DECLARATION FOR THE INPUT PARAMETERS OF THE WEB SERVICE
Public Class Transaccion
    'CHECK THE DECLARATION OF THE XML NODE AND ITS NAMESPACE
    <XmlElement("transaccion", Namespace:="")> _
    Public transaccion As String

    <XmlElement("usuario", Namespace:="")> _
    Public usuario As String

    <XmlElement("password", Namespace:="")> _
    Public password As String

    Public Sub New()
        Me.transaccion = "0"
        Me.usuario = String.Empty
        Me.password = String.Empty
    End Sub

    Public Sub New(ByVal transaccion As String, ByVal usuario As String, ByVal password As String)
        Me.transaccion = transaccion
        Me.usuario = usuario
        Me.password = password
    End Sub

    'HERE YOU DECLARE THE NAMESPACES FOR THE XML ELEMENT
    <XmlNamespaceDeclarations()> _
    Public Property xmlns() As XmlSerializerNamespaces
        Get
            Dim xsn As New XmlSerializerNamespaces()
            xsn.Add("def", "http://DefaultNamespace")

            Return xsn
        End Get
        ' needed for xml serialization 
        Set(ByVal value As XmlSerializerNamespaces)
        End Set
    End Property
End Class

'HERE THERE IS A CLASS DECLARATION FOR THE OUTPUT RESPONSE
Public Class RespuestaXML
    'THIS IS THE SAME AS THE INPUT PARAMETER, THE NODE NAME AND ITS NAMESPACE
    <XmlElement("TransaccionReturn", Namespace:="")> _
    Public Body As String

    Public Sub New()
        Me.Body = "##"
    End Sub

    Public Sub New(ByVal StringReturn As String)
        Me.Body = StringReturn
    End Sub

    'HERE IS THE TRICK, DECLARE THE NAMESPACES FOR THE RESPONSE    
    <XmlNamespaceDeclarations()> _
    Public Property xmlns() As XmlSerializerNamespaces
        Get
            Dim xsn As New XmlSerializerNamespaces()
            xsn.Add("ns1", "http://DefaultNamespace")

            Return xsn
        End Get
        ' needed for xml serialization 
        Set(ByVal value As XmlSerializerNamespaces)
        End Set
    End Property
End Class