Message.GetBody Message.GetBody Message.GetBody Message.GetBody Method

定义

检索此 Message 实例的正文。Retrieves the body of this Message instance.

重载

GetBody<T>() GetBody<T>() GetBody<T>() GetBody<T>()

检索此 Message 实例的正文。Retrieves the body of this Message instance.

GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer)

使用指定的序列化程序检索此 Message 的正文。Retrieves the body of this Message using the specified serializer.

示例

下面的代码示例演示了使用通道工厂发送消息并读取回复的客户端。The following code example shows a client that uses the channel factory to send a message and read the reply.

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
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 方法读取返回的消息的正文。After a message is created, the message body can be read using the GetBody methods on the returned message. 返回的对象将所有子元素包装在 Body 元素内。The returned object encapsulates all the child elements within the Body element. 由于消息的正文是流,因此只能写入或读取一次。Because the body of a message is a stream, it can only be written once, or read once.

使用 JSON 消息时,请使用 GetBody<T>(XmlObjectSerializer) 重载;GetBody<T>() 重载不起作用。Use the GetBody<T>(XmlObjectSerializer) overload when working with JSON messages; the GetBody<T>() overload does not work.

GetBody<T>() GetBody<T>() GetBody<T>() GetBody<T>()

检索此 Message 实例的正文。Retrieves the body of this Message instance.

public:
generic <typename T>
 T GetBody();
public T GetBody<T> ();
member this.GetBody : unit -> 'T
Public Function GetBody(Of T) () As T

类型参数

T

消息的正文。The body of the message.

返回

T T T T

一个类型为 T 的对象,其中包含此消息的正文。An object of type T that contains the body of this message.

注解

Message 实例的正文对应于 SOAP 正文。A Message instance has a body that corresponds to the SOAP body. 可通过调用 XmlDictionaryReaderGetReaderAtBodyContents 的形式来访问它。It can be accessed as an XmlDictionaryReader by calling GetReaderAtBodyContents. 它最初定位在紧靠 <S:Body> 元素后面的位置,并在到达 EOF 元素时返回 </S:Body>It is initially positioned directly after the <S:Body> element, and it returns EOF on reaching the </S:Body> element. 或者,如果希望正文包含序列化对象,则可以改为调用 GetBodyAlternatively, if you expect the body to contain a serialized object, you can call GetBody instead. 应该注意,只能访问消息正文一次,并且只能写入消息一次。You should be aware that a message body can only be accessed once and a message can only be written once. 如果要访问 Body 多次,请使用 CreateBufferedCopy 创建 MessageBuffer 实例。If you want to access the Body multiple times, use CreateBufferedCopy to create a MessageBuffer instance.

使用 JSON 消息时,请使用 GetBody<T>(XmlObjectSerializer) 重载;GetBody<T>() 重载不起作用。Use the GetBody<T>(XmlObjectSerializer) overload when working with JSON messages; the GetBody<T>() overload does not work.

GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer) GetBody<T>(XmlObjectSerializer)

使用指定的序列化程序检索此 Message 的正文。Retrieves the body of this Message using the specified serializer.

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

消息的正文。The body of the message.

参数

serializer
XmlObjectSerializer XmlObjectSerializer XmlObjectSerializer XmlObjectSerializer

用于读取消息的正文的 XmlObjectSerializer 对象。A XmlObjectSerializer object used to read the body of the message.

返回

T T T T

一个类型为 T 的对象,其中包含此消息的正文。An object of type T that contains the body of this message.

异常

注解

Message 实例的正文对应于 SOAP 正文。A Message instance has a body that corresponds to the SOAP body. 可通过调用 XmlDictionaryReaderGetReaderAtBodyContents 的形式来访问它。It can be accessed as an XmlDictionaryReader by calling GetReaderAtBodyContents. 它最初定位在紧靠 <S:Body> 元素后面的位置,并在到达 EOF 元素时返回 </S:Body>It is initially positioned directly after the <S:Body> element, and it returns EOF on reaching the </S:Body> element. 或者,如果希望正文包含序列化对象,则可以改为调用 GetBodyAlternatively, if you expect the body to contain a serialized object, you can call GetBody instead. 应该注意,只能访问消息正文一次,并且只能写入消息一次。You should be aware that a message body can only be accessed once and a message can only be written once. 如果要访问正文多次,请使用 CreateBufferedCopy 创建 MessageBuffer 实例。If you want to access the body multiple times, use CreateBufferedCopy to create a MessageBuffer instance.

使用 JSON 消息时,请使用 GetBody<T>(XmlObjectSerializer) 重载;GetBody<T>() 重载不起作用。Use the GetBody<T>(XmlObjectSerializer) overload when working with JSON messages; the GetBody<T>() overload does not work.

适用于