Gewusst wie: Steuern der Formatierung der Parameter und Rückgabewerte für eine Webdienstmethode

Für das Formatieren von XML-Elementen in Übereinstimmung mit Methodenparametern und Rückgabewerten oder use stellt WSDL (Web Services Description Language) zwei Optionen bereit: SOAP-codiert und literal. .NET Framework steuert diese Auswahl mithilfe von Attributen im Code. Obwohl ASP.NET eine umfassende Architektur zur Steuerung der Formatierung der XML-Elemente bietet, ist die Serialisierungsreihenfolge der Parameter nicht garantiert.

So legen Sie den Parameterformatierungsstil Literal fest

  • Wenden Sie ein SoapDocumentMethod-Attribut auf eine Methode der Proxyklasse an, indem Sie die Use-Eigenschaft auf SoapBindingUse.Literal festlegen.

    Die SoapBindingUse-Enumeration gibt die Parameterformatierungsstile an, die für einen mit ASP.NET erstellten Webdienst verfügbar sind.

    [SoapDocumentMethod(
        "https://www.contoso.com/DocumentLiteral",
        RequestNamespace="https://www.contoso.com",
        ResponseNamespace="https://www.contoso.com",
        Use=SoapBindingUse.Literal)]
    public string DocumentLiteral(Address1 address, bool useZipPlus4) {
    
    <SoapDocumentMethod( _
       "https://www.contoso.com/DocumentLiteral", _
       RequestNamespace:="https://www.contoso.com", _
       ResponseNamespace:="https://www.contoso.com", _
       Use:=SoapBindingUse.Literal)>  _
    Public Function DocumentLiteral(ByVal address As Address1, _
                             ByVal useZipPlus4 As Boolean) As String
    

    Es folgt der XML-Teil der SOAP-Anforderung, die an die DocumentLiteral-Webdienstmethode gesendet wird. Die Parameter befinden sich im Body-Element und sind als unabhängige XML-Dokumente codiert, da sie sich auf ein XSD-Schema beziehen.

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body>
        <DocumentLiteral xmlns="https://www.contoso.com">
          <address>
            <Street>One Microsoft Way</Street>
            <City>Redmond</City>
            <Zip>98052</Zip>
          </address>
          <useZipPlus4>True</useZipPlus4>
        </DocumentLiteral>
      </soap:Body>
    </soap:Envelope>
    

So legen Sie den Parameterformatierungsstil Encoded fest

  • Wenden Sie ein SoapDocumentMethod-Attribut oder ein SoapRpcMethod-Attribute auf die Methode der Proxyklasse an, indem Sie die Use-Eigenschaft auf SoapBindingUse.Encoded festlegen.

    Im Gegensatz zum Parameterformatierungsstil Literal kann der Parameterformatierungsstil Encoded in Verbindung mit beiden Formatierungsstilen für Webdienstmethoden verwendet werden. Nähere Informationen zu den Formatierungsstilen für Webdienstmethoden finden Sie unter .NET Framework-Unterstützung für SOAP-Formate

    [SoapDocumentMethod("https://www.contoso.com/DocumentEncoded",
                        RequestNamespace="https://www.contoso.com",
                        ResponseNamespace="https://www.contoso.com",
                        Use=SoapBindingUse.Encoded)]
    public string DocumentEncoded(Address address, bool useZipPlus4) {
    
    <SoapDocumentMethod("https://www.contoso.com/DocumentEncoded", _
                        RequestNamespace:="https://www.contoso.com", _
                        ResponseNamespace:="https://www.contoso.com", _
                        Use:=SoapBindingUse.Encoded)>  _
    Public Function DocumentEncoded(ByVal address As Address, _ 
                      ByVal useZipPlus4 As Boolean) As String
    

    Es folgt der XML-Teil der SOAP-Anforderung, die an die DocumentEncoded-Dienstmethode gesendet wird. Beachten Sie, dass die Darstellung der Parameter deutlich von der Darstellung im Formatierungsstil Literal abweicht, da sie gemäß den in Abschnitt 5 der SOAP-Spezifikation festgelegten Codierungsregeln formatiert wurden. Besondere Beachtung verdient der address-Parameter, der keinen einfachen Datentyp darstellt.

    <?xml version="1.0" encoding="utf-8"?>
    <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:soapenc="https://schemas.xmlsoap.org/soap/encoding/"
    xmlns:tns="https://www.contoso.com"
    xmlns:tnsTypes="https://www.contoso.com/encodedTypes"
    xmlns:wsdl="https://schemas.xmlsoap.org/wsdl/"
    xmlns:soap="https://schemas.xmlsoap.org/soap/envelope/">
      <soap:Body soap:encodingStyle="https://schemas.xmlsoap.org/soap/encoding/">
        <tnsTypes:DocumentEncoded>
          <address href="#1" />
          <useZipPlus4>boolean</useZipPlus4>
        </tnsTypes:DocumentEncoded>
        <tnsTypes:Address id="1">
          <Street id="2">string</Street>
          <City id="3">string</City>
          <Zip id="4">string</Zip>
        </tnsTypes:Address>
      </soap:Body>
    </soap:Envelope>
    

Siehe auch

Referenz

SoapDocumentMethodAttribute
SoapRpcMethodAttribute

Weitere Ressourcen

Anpassen der Formatierung von SOAP-Nachrichten

Footer image

Copyright © 2007 by Microsoft Corporation. Alle Rechte vorbehalten.