HOW TO:為整個 Web 服務修改預設 SOAP 格式

這個程序描述如何修改整個 Web 服務的預設 SOAP 格式。

若要設定 Web 服務的預設方法格式化樣式

  • SoapRpcService 屬性或 SoapDocumentService 屬性套用至實作 Web 服務的類別。

    下列範例程式碼會將方法格式化樣式設定為 Document、將預設參數格式化設定為 Literal, ,而且指定參數必須封裝在單一項目中。

    <%@ WebService Language="C#" Class="SoapDocumentServiceSample" %>
     using System.Web.Services;
     using System.Web.Services.Protocols;
     using System.Web.Services.Description;
    
    [SoapDocumentService(Use=SoapBindingUse.Literal,
                         ParameterStyle=SoapParameterStyle.Wrapped)]
    [WebService(Namespace="https://www.contoso.com")]
    public class SoapDocumentServiceSample  
    {
        [ WebMethod ]
        public string UseDefaultEncoding(Address MyAddress, 
                                         bool useZipPlus4) 
        {
         return "Use the default encodings for this Web service.";
        }
    }
    
    <%@ WebService Language="VB" Class="SoapDocumentServiceSample" %>
    Imports System.Web.Services
    Imports System.Xml.Serialization
    Imports System.Web.Services.Protocols
    Imports System.Web.Services.Description
    
    < SoapDocumentService(Use := SoapBindingUse.Literal, _
                          ParameterStyle := SoapParameterStyle.Wrapped)> _
    Public Class SoapDocumentServiceSample
      < WebMethod > _
      Public Function UseDefaultEncoding(MyAddress as Address, _
                                         useZipPlus4 As Boolean) As String 
         Return "Use the default formattings for this Web service."
      End Function
    End Class 
    

    UseDefaultEncoding Web 服務方法預期的 SOAP 要求的 XML 部分如下。

    <?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>
        <UseDefaultEncoding xmlns="https://www.contoso.com">
          <MyAddress>
            <Street>string</Street>
            <City>string</City>
            <Zip>string</Zip>
          </MyAddress>
          <useZipPlus4>boolean</useZipPlus4>
        </UseDefaultEncoding>
      </soap:Body>
    </soap:Envelope>
    

請參閱

參考

SoapDocumentServiceAttribute
SoapRpcServiceAttribute

其他資源

自訂 SOAP 訊息格式

Footer image

Copyright © 2007 by Microsoft Corporation. All rights reserved.