未包裝的訊息
此範例示範未包裝的訊息。根據預設,訊息本文會格式化為包裝服務作業的參數。下列範例說明對包裝模式中對 ICalculator
服務的 Add
要求訊息。
<s:Envelope
xmlns:s=http://www.w3.org/2003/05/soap-envelope
xmlns:a="https://schemas.xmlsoap.org/ws/2005/08/addressing">
<s:Header>
…
</s:Header>
<s:Body>
<Add xmlns="http://Microsoft.ServiceModel.Samples">
<n1>100</n1>
<n2>15.99</n2>
</Add>
</s:Body>
</s:Envelope>
訊息本文中的 <Add>
項目會包裝 n1
和 n2
參數。相反地,下列範例說明未包裝模式中的對等訊息。
<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="https://schemas.xmlsoap.org/ws/2005/08/addressing">
<s:Header>
….
</s:Header>
<s:Body>
<n1 xmlns="http://Microsoft.ServiceModel.Samples">100</n1>
<n2 xmlns="http://Microsoft.ServiceModel.Samples">15.99</n2>
</s:Body>
</s:Envelope>
</MessageLogTraceRecord>
未包裝的訊息不會包裝在包含項目中的 n1
和 n2
參數,它們是 SOAP 本文項目的直接子系。
注意: |
---|
此範例的安裝程序與建置指示位於本主題的結尾。 |
在此範例中,未包裝訊息的建立方式是將 MessageContractAttribute 套用至服務作業參數型別和傳回值型別,如下列範例程式碼所示。
[ServiceContract(Namespace="http://Microsoft.ServiceModel.Samples")]
public interface ICalculator
{
[OperationContract]
ResponseMessage Add(RequestMessage request);
[OperationContract]
ResponseMessage Subtract(RequestMessage request);
[OperationContract]
ResponseMessage Multiply(RequestMessage request);
[OperationContract]
ResponseMessage Divide(RequestMessage request);
}
//setting IsWrapped to false means the n1 and n2
//members will be direct children of the soap body element
[MessageContract(IsWrapped = false)]
public class RequestMessage
{
[MessageBodyMember]
private double n1;
[MessageBodyMember]
private double n2;
//…
}
//setting IsWrapped to false means the result
//member will be a direct child of the soap body element
[MessageContract(IsWrapped = false)]
public class ResponseMessage
{
[MessageBodyMember]
private double result;
//…
}
為了可讓您看到正在傳送和接收的訊息,此範例使用追蹤。此外,WSHttpBinding 已設定為不帶安全性,可減少其記錄的訊息數量。
使用Service Trace Viewer Tool可以檢視產生的追蹤記錄 (c:\logs\Message.log)。若要檢視訊息內容,請同時選取服務追蹤檢視器工具左窗格和右窗格中的 [訊息]。此範例中的追蹤記錄是設定為產生至 C:\LOGS 資料夾中。請先建立這個資料夾,然後再執行範例並給予使用者這個目錄的網路服務寫入權限。
若要設定、建置及執行範例
建立用於記錄訊息的 C:\LOGS 目錄。給予使用者這個目錄的網路服務寫入權限。
若要建置方案的 C# 或 Visual Basic .NET 版本,請遵循建置 Windows Communication Foundation 範例中的指示。
若要在單一或跨電腦的組態中執行本範例,請遵循執行 Windows Communication Foundation 範例中的指示。
Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.