未包装的示例演示未包装的消息。 默认情况下,消息正文被设置为格式化,以便将服务操作的参数进行封装。 以下示例演示了在包装模式下向 ICalculator
服务发出的 Add
请求消息。
<s:Envelope
xmlns:s="http://www.w3.org/2003/05/soap-envelope"
xmlns:a="http://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="http://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>
未包装的消息不会包装包含元素中的 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 已配置无安全性,以减少日志消息数。
可以使用 服务跟踪查看器工具(SvcTraceViewer.exe)查看生成的跟踪日志(c:\logs\Message.log)。 若要查看消息内容,请在服务跟踪查看器工具的左侧窗格和右窗格中选择 “消息 ”。 此示例中的跟踪日志配置为生成到 C:\LOGS 文件夹中。 在运行示例之前创建此文件夹,并为用户提供此目录的网络服务写入权限。
设置、生成和运行示例
确保已为 Windows Communication Foundation 示例 执行One-Time 安装过程。
创建用于记录消息的 C:\LOGS 目录。 将此目录的写入权限授予网络服务用户。
若要生成解决方案的 C# 或 Visual Basic .NET 版本,请按照 生成 Windows Communication Foundation 示例中的说明进行操作。
若要在单台计算机或跨计算机配置中运行示例,请按照 运行 Windows Communication Foundation 示例中的说明进行操作。