.NET Web服务。(ASMX)如何调整请求和响应消息?

Bre*_*ett 2 vb.net web-services asmx

我正在构建一个Web服务来接受公司的通知。该公司通知我该服务的结构不正确,并向我提供了可运行的Web服务的.asmx。看起来像这样:

请求:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <AuthenticationInfo
           xmlns="http://www.usps.com/postalone/services/UserAuthenticationSchema">
      <UserId xmlns="">string</UserId>
      <UserPassword xmlns="">string</UserPassword>
    </AuthenticationInfo>
    <fullServiceAddressCorrectionNotification
            xmlns="http://www.usps.com/postalone/services/POCustomerMailXMLServices">string</fullServiceAddressCorrectionNotification>
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

响应:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <FullServiceNixieDetailNotificationResponse
         xmlns="http://idealliance.org/maildat/Specs/md091/mailxml70C/mailxml">
      <FullServiceNixieDetailNotificationResult>string</FullServiceNixieDetailNotificationResult>
    </FullServiceNixieDetailNotificationResponse>
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

我的看起来像这样:请求:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <FullServiceNixieDetailNotification
          xmlns="http://idealliance.org/maildat/Specs/md091/mailxml70C/mailxml">
      <AuthenticationInfo>
        <UserID>string</UserID>
        <Password>string</Password>
      </AuthenticationInfo>
      <fullServiceAddressCorrectionNotification>string</fullServiceAddressCorrectionNotification>
    </FullServiceNixieDetailNotification>
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

响应:

<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:xsd="http://www.w3.org/2001/XMLSchema"
       xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
  <soap12:Body>
    <notificationResponse
       xmlns="http://www.usps.com/postalone/services/POCustomerMailXMLServices">string</notificationResponse>
  </soap12:Body>
</soap12:Envelope>
Run Code Online (Sandbox Code Playgroud)

如您所见,我的东西包裹在一个我需要摆脱的额外元素中。Unfortunatley,我不确定如何执行此操作。我的Web服务代码如下:

Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Data.SqlClient

Public Structure AuthenticationInfoType
    Dim UserID As String
    Dim Password As String
End Structure

' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://idealliance.org/maildat/Specs/md091/mailxml70C/mailxml")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class USPSMailXML
    Inherits System.Web.Services.WebService  

    <WebMethod()> _
    Public Function FullServiceNixieDetailNotification(ByVal AuthenticationInfo As AuthenticationInfoType, ByVal fullServiceAddressCorrectionNotification As String) As String

        'Some code here

        Return "MyResponse"

    End Function
Run Code Online (Sandbox Code Playgroud)

Che*_*eso 5

在解决实际问题之前,先对一些东西进行整理。

  1. 您应该做的第一件事就是远离ASMX并使用WCF。如果您还不了解WCF,将为您提供学习曲线。但是我在猜测-别误会-您不是ASMX的真正专家。如果是这样,请不要花时间成为 ASMX的专家,ASMX是昨天的技术;花一些时间学习WCF。对您和您的公司都更好。

  2. 只是为了弄清楚术语,您显示的前两个代码既不是ASMX,也不是WSDL。它们是实际的线级消息。

  3. 不管您选择哪种实施技术,让我告诉您,您显示的示例传入和传出消息非常奇怪。首先,它不符合WSI BP1.0:传入消息元素未包装在顶级元素中。为什么不?然而,该响应消息包裹在一个顶层元素。为什么?不一致似乎是任意的。此外,元素名称全都是TitleCase,只有一个除外-fullServiceAddressCorrectionNotification,它使用camelCase。为什么?此外,传入消息是USPS命名空间,但是参数使用null命名空间。??谁不?这是合法的,但似乎vvvverrrry奇。最后,响应上的xml名称空间与传入消息上的xml名称空间完全不同。(我可以想象这最后一个理由很好)。我收集到您不负责设计消息。但是这些不一致向我发出了危险信号。这就是我所谓的“ 烦人的消息”设计。


好吧,所有的一切都没有了……

有一种方法可以使用SoapDocumentMethodAttribute和XmlElement属性的战略布局以及现有的WebMethod属性来在ASMX中调整请求和响应。SoapDocumentMethodAttribute允许您访问旋钮和杠杆,以指定传入和传出消息的元素名称和名称空间。

这应该使您非常接近:

<%@
  WebService
  Language="VB"
  Class="USPSMailXML"
%>

Imports System
Imports System.Web.Services
Imports System.Web.Services.Protocols
Imports System.ComponentModel
Imports System.Diagnostics
Imports System.Xml.Serialization


Public Structure AuthenticationInfoType
    <XmlElement(Namespace := "")> _
    Dim UserID As String
    <XmlElement(Namespace := "")> _
    Dim Password As String
End Structure


Public Structure NixieResponse
    <XmlElement("FullServiceNixieDetailNotificationResult")> _
    Dim Message As String
End Structure


' Must not claim WSI compliance; there are multiple toplevel elements in the Request body
'
'<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<System.Web.Services.WebService(Namespace:="http://idealliance.org/maildat/Specs/md091/mailxml70C/mailxml"), _
       System.Web.Services.WebServiceBinding(ConformsTo := WsiProfiles.None), _
       ToolboxItem(False)> _
Public Class USPSMailXML
    Inherits System.Web.Services.WebService

    <WebMethod(), _
     SoapDocumentMethod( _
                        Action := "", _
                        RequestElementName := "AuthenticationInfo", _
                        ResponseElementName := "FullServiceNixieDetailNotificationResponse", _
                        RequestNamespace := "http://www.usps.com/postalone/services/UserAuthenticationSchema", _
                        ResponseNamespace := "http://idealliance.org/maildat/Specs/md091/mailxml70C/mailxml", _
                        Use := System.Web.Services.Description.SoapBindingUse.Literal, _
                        ParameterStyle := System.Web.Services.Protocols.SoapParameterStyle.Bare)> _
    Public Function FullServiceNixieDetailNotification(ByVal AuthenticationInfo As AuthenticationInfoType, _
                                                       <XmlElement(Namespace := "http://www.usps.com/postalone/services/POCustomerMailXMLServices")> _
                                                       ByVal fullServiceAddressCorrectionNotification As String) _
              As  <XmlElement("FullServiceNixieDetailNotificationResponse")> NixieResponse

        Dim r As New NixieResponse
        r.Message = "My Response"

        Return r

    End Function

End Class
Run Code Online (Sandbox Code Playgroud)

我必须将其标记为ParameterStyle = Bare,因为传入的消息具有2个顶级元素。这意味着传出的邮件也是Bare,在您的情况下不是您想要的。因此,传出的消息(只是一个字符串)-我必须将其包装到一个结构中,以便按照您显示的方式获得形状。

我已经评论了传入和传出消息的设计。如果消息设计更加“理智”,则其中的某些扭曲将是不必要的。