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

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

我正在研究一个最初使用 Axis 用 Ja​​va 开发的遗留 Web 服务,它的响应是:

<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 宣言?

最佳答案

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

关于asp.net - 在.NET Web 服务中将soap 前缀更改为soapenv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25875952/

相关文章:

java - 使用 Apache CXF 发送 FastInfoset 请求时,我可以在日志中包含 XML 吗?

c# - 将经典 ASP 应用程序转换为 ASP.NET 有哪些有用的策略

c# - 结合两个正则表达式进行 ASP.NET MVC 数据注释

javascript - 跨用户控件属性设置和持久值

c# - 更新命令不起作用?

.net - 使用 SSH.NET (VB.NET) 将文件下载到字节数组

c# - Bitwise Shift - 在 C#.net 与 PHP 中获得不同的结果

vb.net - YouTube 签名解密以及 VB.Net 的工作示例

python - azure ml 实验返回与 webservice 不同的结果

web-services - Grails:公开 LoginController 以进行编程登录