未包装的消息

此示例演示未包装的消息。默认情况下,消息正文的格式设置为对服务操作的参数进行包装。下面的示例演示一个包装模式下的对 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> 元素包装 n1n2 参数。相反,下面的示例则显示未包装模式下的等效消息。

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

未包装的消息不会包装包含元素中的 n1n2 参数,这些参数是 SOAP 正文元素的直接子级。

ms750528.note(zh-cn,VS.100).gif注意:
本主题的末尾介绍了此示例的设置过程和生成说明。

在此示例中,通过向服务操作参数类型和返回值类型应用 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 文件夹。请在运行此示例之前创建该文件夹,并为用户赋予对该目录的“网络服务”写权限。

设置、生成和运行示例

  1. 请确保已经执行了 Windows Communication Foundation 示例的一次性安装过程

  2. 创建一个 C:\LOGS 目录,用于记录消息。向用户授予对该目录的“网络服务”写权限。

  3. 若要生成 C# 或 Visual Basic .NET 版本的解决方案,请按照生成 Windows Communication Foundation 示例中的说明进行操作。

  4. 若要用单机配置或跨计算机配置来运行示例,请按照Running the Windows Communication Foundation Samples中的说明进行操作。

ms750528.Important(zh-cn,VS.100).gif 注意:
您的计算机上可能已安装这些示例。在继续操作之前,请先检查以下(默认)目录:

<安装驱动器>:\WF_WCF_Samples

如果此目录不存在,请访问针对 .NET Framework 4 的 Windows Communication Foundation (WCF) 和 Windows Workflow Foundation (WF) 示例(可能为英文网页),下载所有 Windows Communication Foundation (WCF) 和 WF 示例。此示例位于以下目录。

<安装驱动器>:\WF_WCF_Samples\WCF\Basic\Contract\Message\Unwrapped