HOW TO:控制 Web 服務方法的整體 SOAP 主體格式
本主題專門說明舊有技術。 應該使用下列建立 XML Web Service 及 XML Web Service 用戶端: Windows Communication Foundation.
針對整體 SOAP 主體格式 (或樣式),Web 服務描述語言 (WSDL) 提供兩個選擇:RPC 和文件。.NET Framework 可以在程式碼中使用屬性控制這些選擇。
若要指定文件格式化樣式
在 Proxy 類別中呼叫適用 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
使用文件格式化樣式,在定義 SOAP 要求和 SOAP 回應的服務描述中會定義 XSD 結構描述。下列是
DocumentWrappedLiteral
Web 服務方法的 SOAP 要求服務描述摘錄。因為DocumentWrappedLiteral
Web 服務方法的第一個參數是類別,而且已指定 Literal 參數格式化樣式,所以會建立address
型別的 XSD 結構描述。<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 格式化樣式
在 Proxy 類別中呼叫適用 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
在先前範例中,XSD 結構描述不是嚴格定義在
Rpc
方法的 SOAP 要求或 SOAP 回應的服務描述中,只是組成部分。因此,請查看Rpc
方法的 SOAP 要求,注意參數是封裝在一個項目中,而且已使用 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