【问题标题】:Change soap prefix to soapenv in .NET Web Service在 .NET Web 服务中将 soap 前缀更改为 soapenv
【发布时间】:2014-09-16 18:26:45
【问题描述】:

我正在开发一个旧版 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 声明?

【问题讨论】:

  • 我也有同样的问题??有什么解决办法吗??
  • @VishwanathMishra 检查我的答案我能够解决问题。
  • 将soap转换为soapenv的地方是哪里写的??
  • 实际上,该前缀没有添加到响应中,但它实际上是有效的,Java 客户端可以毫无问题地识别它。

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


【解决方案1】:
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

【讨论】:

  • 将soap转换为soapenv的地方,我无法理解你的代码,请你在这里解决我的问题stackoverflow.com/questions/27957656/…
  • 这段代码为 Java 客户端创建了一个有效的 SOAP 响应,它们没有任何变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-17
  • 2018-12-17
  • 2021-06-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多