方法 : Web サービス メソッドで SOAP 本文全体の書式を制御する

このトピックの対象は、レガシ テクノロジに特定されています。XML Web サービスと XML Web サービス クライアントは以下を使用して作成してください。 Windows Communication Foundation.

Web サービス記述言語 (WSDL: Web Service Description Language) では、SOAP 本文全体の書式 (スタイル) として RPC および document のいずれかを選択できます。.NET Framework では、これらの選択を属性を使用してコードで制御します。

Document 書式スタイルを指定するには

  1. 該当の Web サービス メソッドを呼び出すプロキシ クラスのメソッドに SoapDocumentMethod 属性または SoapRpcMethod を適用します。

    ASP.NET のサポートを使用して作成した Web サービスでは、Literal パラメーター書式スタイルと Encoded パラメーター書式スタイルの両方を使用します。Document メソッド書式スタイルと Literal パラメーター書式スタイルを組み合わせた例を次に示します。

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

    Document 書式スタイルでは、XSD スキーマはサービスの説明内で定義され、SOAP 要求と SOAP 応答の両方を定義します。DocumentWrappedLiteral Web サービス メソッドの SOAP 要求に対するサービスの説明の抜粋を次に示します。DocumentWrappedLiteral Web サービス メソッドの最初のパラメーターはクラスであり、Literal パラメーター書式スタイルが指定されているため、XSD スキーマで address 型が作成されます。

    <s:element name="DocumentWrappedLiteral">
      <s:complexType>
        <s:sequence>
           <s:element minOccurs="1" maxOccurs="1" name="MyAddress"
                      nillable="true" type="s0:Address" /> 
           <s:element minOccurs="1" maxOccurs="1" name="useZipPlus4"
                      type="s:boolean" /> 
        </s:sequence>
      </s:complexType>
    </s:element>
    
    <s:complexType name="Address">
       <s:sequence>
          <s:element minOccurs="1" maxOccurs="1" name="Street"
                     nillable="true" type="s:string" /> 
          <s:element minOccurs="1" maxOccurs="1" name="City"
                     nillable="true" type="s:string" /> 
          <s:element minOccurs="1" maxOccurs="1" name="Zip" nillable="true"
                     type="s:string" /> 
       </s:sequence>
    </s:complexType>
    

    サービスの説明で定義された XSD スキーマでは、DocumentWrappedLiteral サービス メソッドに対する SOAP 要求の XML 部分は次のようになります。SOAP 要求内の Body 要素の下にある XML 要素は、XSD スキーマで定義されている要素と一致します。

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

RPC 書式スタイルを指定するには

  1. 該当の Web サービス メソッドを呼び出すプロキシ クラスのメソッドに SoapRpcMethod 属性を適用します。

    [SoapRpcMethodAttribute("https://www.contoso.com/Rpc",
                            RequestNamespace="https://www.contoso.com",
                            ResponseNamespace="https://www.contoso.com")]
    public Address Rpc(Address address, bool useZipPlus4) {
    
    <SoapRpcMethodAttribute("https://www.contoso.com/Rpc", _
                            RequestNamespace:="https://www.contoso.com", _
                            ResponseNamespace:="https://www.contoso.com")> _
    Public Function Rpc(ByVal address As Address, _
                        ByVal useZipPlus4 As Boolean) As Address
    

    前の例では、Rpc メソッドに対する SOAP 要求または SOAP 応答のいずれについても、XSD スキーマはサービスの説明で厳密に定義されておらず、単にそれらを構成する要素になっています。したがって、Rpc メソッドの SOAP 要求を見ると、パラメーターは 1 つの要素にカプセル化され、Encoded パラメーター書式を使用してエンコードされています。

    <?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/">
        <tns:Rpc>
          <address href="#1" />
          <useZipPlus4>boolean</useZipPlus4>
        </tns:Rpc>
        <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> 
    

参照

リファレンス

SoapDocumentMethodAttribute
SoapRpcMethodAttribute

その他のリソース

SOAP メッセージの書式のカスタマイズ