Message.GetBody 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
检索此 Message 实例的正文。
重载
| 名称 | 说明 |
|---|---|
| GetBody<T>() |
检索此 Message 实例的正文。 |
| GetBody<T>(XmlObjectSerializer) |
使用指定的序列化程序检索此 Message 正文。 |
示例
下面的代码示例演示了一个客户端,该客户端使用通道工厂发送消息并读取答复。
using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.Runtime.Serialization;
namespace ConsoleApplication1
{
class client
{
static void RunClient()
{
//Step1: create a binding with just HTTP
CustomBinding binding = new CustomBinding();
binding.Elements.Add(new HttpTransportBindingElement());
//Step2: use the binding to build the channel factory
IChannelFactory<IRequestChannel> factory =
binding.BuildChannelFactory<IRequestChannel>(
new BindingParameterCollection());
//open the channel factory
factory.Open();
//Step3: use the channel factory to create a channel
IRequestChannel channel = factory.CreateChannel(
new EndpointAddress("http://localhost:8080/channelapp"));
channel.Open();
//Step4: create a message
Message requestmessage = Message.CreateMessage(
MessageVersion.Soap12WSAddressing10,
"http://contoso.com/someaction",
"This is the body data");
//send message
Message replymessage = channel.Request(requestmessage);
Console.WriteLine("Reply message received");
Console.WriteLine("Reply action: {0}",
replymessage.Headers.Action);
string data = replymessage.GetBody<string>();
Console.WriteLine("Reply content: {0}", data);
//Step5: don't forget to close the message
requestmessage.Close();
replymessage.Close();
//don't forget to close the channel
channel.Close();
//don't forget to close the factory
factory.Close();
}
public static void Main()
{
Console.WriteLine("Press [ENTER] when service is ready");
Console.ReadLine();
RunClient();
Console.WriteLine("Press [ENTER] to exit");
Console.ReadLine();
}
}
}
Imports System.Collections.Generic
Imports System.Text
Imports System.ServiceModel
Imports System.ServiceModel.Channels
Imports System.Runtime.Serialization
Namespace ConsoleApplication1
Friend Class client
Private Shared Sub RunClient()
'Step1: create a binding with just HTTP
Dim binding As New CustomBinding()
binding.Elements.Add(New HttpTransportBindingElement())
'Step2: use the binding to build the channel factory
Dim factory As IChannelFactory(Of IRequestChannel) = binding.BuildChannelFactory(Of IRequestChannel)(New BindingParameterCollection())
'open the channel factory
factory.Open()
'Step3: use the channel factory to create a channel
Dim channel As IRequestChannel = factory.CreateChannel(New EndpointAddress("http://localhost:8080/channelapp"))
channel.Open()
'Step4: create a message
Dim requestmessage As Message = Message.CreateMessage(MessageVersion.Soap12WSAddressing10, "http://contoso.com/someaction", "This is the body data")
'send message
Dim replymessage As Message = channel.Request(requestmessage)
Console.WriteLine("Reply message received")
Console.WriteLine("Reply action: {0}", replymessage.Headers.Action)
Dim data = replymessage.GetBody(Of String)()
Console.WriteLine("Reply content: {0}", data)
'Step5: don't forget to close the message
requestmessage.Close()
replymessage.Close()
'don't forget to close the channel
channel.Close()
'don't forget to close the factory
factory.Close()
End Sub
Public Shared Sub Main()
Console.WriteLine("Press [ENTER] when service is ready")
Console.ReadLine()
RunClient()
Console.WriteLine("Press [ENTER] to exit")
Console.ReadLine()
End Sub
End Class
End Namespace
注解
创建消息后,可以使用返回的消息上的方法读取 GetBody 消息正文。 返回的对象封装元素中的所有 Body 子元素。 由于消息正文是一个流,因此只能写入一次或读取一次。
使用 JSON 消息时使用 GetBody<T>(XmlObjectSerializer) 重载; GetBody<T>() 重载不起作用。
GetBody<T>()
- Source:
- Message.cs
- Source:
- Message.cs
- Source:
- Message.cs
检索此 Message 实例的正文。
public:
generic <typename T>
T GetBody();
public T GetBody<T>();
member this.GetBody : unit -> 'T
Public Function GetBody(Of T) () As T
类型参数
- T
邮件的正文。
返回
包含此消息正文的类型 T 的对象。
注解
Message实例具有对应于 SOAP 正文的正文。 可以通过调用XmlDictionaryReader来访问GetReaderAtBodyContents它。 它最初直接定位在<S:Body>元素之后,在到达EOF元素时返回</S:Body>。 或者,如果希望正文包含序列化的对象,可以改为调用 GetBody 。 应注意消息正文只能访问一次,一条消息只能写入一次。 如果要多次访问 Body ,请使用 CreateBufferedCopy 创建 MessageBuffer 实例。
使用 JSON 消息时使用 GetBody<T>(XmlObjectSerializer) 重载; GetBody<T>() 重载不起作用。
适用于
GetBody<T>(XmlObjectSerializer)
- Source:
- Message.cs
- Source:
- Message.cs
- Source:
- Message.cs
使用指定的序列化程序检索此 Message 正文。
public:
generic <typename T>
T GetBody(System::Runtime::Serialization::XmlObjectSerializer ^ serializer);
public T GetBody<T>(System.Runtime.Serialization.XmlObjectSerializer serializer);
member this.GetBody : System.Runtime.Serialization.XmlObjectSerializer -> 'T
Public Function GetBody(Of T) (serializer As XmlObjectSerializer) As T
类型参数
- T
邮件的正文。
参数
- serializer
- XmlObjectSerializer
XmlObjectSerializer用于读取消息正文的对象。
返回
包含此消息正文的类型 T 的对象。
例外
serializer 是 null。
注解
Message实例具有对应于 SOAP 正文的正文。 可以通过调用XmlDictionaryReader来访问GetReaderAtBodyContents它。 它最初直接定位在<S:Body>元素之后,在到达EOF元素时返回</S:Body>。 或者,如果希望正文包含序列化的对象,可以改为调用 GetBody 。 应注意消息正文只能访问一次,一条消息只能写入一次。 如果要多次访问正文,请使用 CreateBufferedCopy 创建 MessageBuffer 实例。
使用 JSON 消息时使用 GetBody<T>(XmlObjectSerializer) 重载; GetBody<T>() 重载不起作用。