方法 : Web サービス メソッドのパラメーターを別の要素で囲むかどうかを制御する

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

Web サービス メソッドのパラメーターまたは戻り値は、SOAP メッセージの Body 要素内の親 XML 要素内に自動的にカプセル化するか、Web サービス記述言語 (WSDL: Web Service Description Language) ドキュメント内のメッセージの part 要素に直接バインドできます。.NET Framework ではこれら 2 つの方法をそれぞれラップおよびベアと呼び、属性を使用してそれらを制御します。

パラメーターを 1 つの XML 要素内にカプセル化するよう指定するには

  1. 該当する Web サービス メソッドを呼び出すプロキシ クラスのメソッドに SoapDocumentMethod 属性を適用し、ParameterStyle プロパティを Wrapped に設定します。

    ParameterStyle プロパティを Wrapped に設定するコード例を次に示します。この例では、パラメーター書式スタイルを Literal にも設定します。

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

    SOAP 要求の XML 部分で、Web サービス メソッド名に基づく名前が既定で付けられた要素にパラメーターがカプセル化されます。

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

    SOAP 応答の XML 部分で、結果を含む Web サービス メソッドの out パラメーターが要素にカプセル化されます。カプセル化する要素の名前は、既定では Web サービス メソッドの名前に Response を付けた名前になります。

    <?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>
        <DocumentWrappedLiteralResponse xmlns="https://www.contoso.com">
          <DocumentWrappedLiteralResult>string
          </DocumentWrappedLiteralResult>
        </DocumentWrappedLiteralResponse>
      </soap:Body>
    </soap:Envelope>
    

パラメーターが Body 要素の直後に続くよう指定するには

  1. 該当する Web サービス メソッドを呼び出すプロキシ クラスのメソッドに SoapDocumentMethod 属性を適用し、ParameterStyle プロパティを Bare に設定します。

    Wsdl.exe で生成した次の例では、ParameterStyleBare に設定し、パラメーター書式スタイルを Literal に設定します。すべてのパラメーターをカプセル化する要素では名前空間を指定できないため、名前空間は各パラメーターおよび戻り値に対して個別に指定する必要があります。名前空間を個別に指定するには、各パラメーターと戻り値に XmlElementAttribute を適用し、Namespace プロパティを設定します。

    [SoapDocumentMethod(
         "https://www.contoso.com/DocumentBareLiteral",
         Use=SoapBindingUse.Literal,
         ParameterStyle=SoapParameterStyle.Bare)]
    [return: XmlElement(Namespace="https://www.contoso.com",                    IsNullable=true)]
    public string DocumentBareLiteral(
       [XmlElement(Namespace="https://www.contoso.com",
                         IsNullable=true)] 
       Address1 MyAddress, 
       [XmlElement(Namespace="https://www.contoso.com",
                IsNullable=false)] 
       bool useZipPlus4) {
    
    <SoapDocumentMethod( _
         https://www.contoso.com/DocumentBareLiteral", _
         Use:=SoapBindingUse.Literal, _
         ParameterStyle:= SoapParameterStyle.Bare)> _
    Public Function DocumentBareLiteral( _
       ByVal <XmlElement([Namespace]:="https://www.contoso.com", _
                          IsNullable:=true)> _
       MyAddress As Address1, _
       ByVal <XmlElement([Namespace]:="https://www.contoso.com", _
                          IsNullable:=false)> _
       useZipPlus4 As Boolean) _
       As <XmlElement([Namespace]:="https://www.contoso.com", _
                      IsNullable:=true)> _
       String
    

    SOAP 要求内でパラメーターのマップ先となる XML 要素が Body 要素のすぐ後に続き、それぞれが名前空間を指定します。

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

    戻り値を含む out パラメーターは、SOAP 応答内の Body 要素に続く XML 要素にマップされます。戻り値の要素名は、既定では Web サービス メソッドの名前に Result というサフィックスが付いた名前になります。

    <?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>
        <DocumentBareLiteralResult xmlns="https://www.contoso.com">
           string</DocumentBareLiteralResult>
      </soap:Body>
    </soap:Envelope>
    

参照

リファレンス

SoapDocumentMethodAttribute
SoapRpcMethodAttribute

その他のリソース

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