Message.Body Propiedad

Definición

Obtiene o establece el contenido del mensaje.Gets or sets the content of the message.

public:
 property System::Object ^ Body { System::Object ^ get(); void set(System::Object ^ value); };
[System.ComponentModel.Browsable(false)]
public object Body { get; set; }
[<System.ComponentModel.Browsable(false)>]
member this.Body : obj with get, set
Public Property Body As Object

Valor de propiedad

Object

Objeto que especifica el contenido del mensaje.An object that specifies the message contents. El objeto puede ser una cadena, una fecha, una divisa, un número, una matriz de bytes o cualquier objeto administrado.The object can be a string, a date, a currency, a number, an array of bytes, or any managed object.

Atributos

Excepciones

La propiedad Formatter es null.The Formatter property is null.

o bien-or- La cola de mensajes se filtra para omitir la propiedad Body.The message queue is filtered to ignore the Body property.

Ejemplos

En el ejemplo de código siguiente se envían dos mensajes de diferentes prioridades a la cola y se recuperan posteriormente.The following code example sends two messages of different priorities to the queue, and retrieves them subsequently.


#using <system.dll>
#using <system.messaging.dll>

using namespace System;
using namespace System::Messaging;

/// <summary>
/// Provides a container class for the example.
/// </summary>
ref class MyNewQueue
{
   //**************************************************
   // Sends a string message to a queue.
   //**************************************************
public:
   void SendMessage( MessagePriority priority, String^ messageBody )
   {
      // Connect to a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Create a new message.
      Message^ myMessage = gcnew Message;
      if ( priority > MessagePriority::Normal )
      {
         myMessage->Body = "High Priority: {0}",messageBody;
      }
      else
      {
         myMessage->Body = messageBody;
      }

      // Set the priority of the message.
      myMessage->Priority = priority;

      // Send the Order to the queue.
      myQueue->Send( myMessage );

      return;
   }

   //**************************************************
   // Receives a message.
   //**************************************************
   void ReceiveMessage()
   {
      // Connect to the a queue on the local computer.
      MessageQueue^ myQueue = gcnew MessageQueue( ".\\myQueue" );

      // Set the queue to read the priority. By default, it
      // is not read.
      myQueue->MessageReadPropertyFilter->Priority = true;

      // Set the formatter to indicate body contains a String^.
      array<Type^>^ p = gcnew array<Type^>(1);
      p[ 0 ] = String::typeid;
      myQueue->Formatter = gcnew XmlMessageFormatter( p );
      try
      {
         // Receive and format the message. 
         Message^ myMessage = myQueue->Receive();

         // Display message information.
         Console::WriteLine( "Priority: {0}",
            myMessage->Priority );
         Console::WriteLine( "Body: {0}",
            myMessage->Body );
      }
      catch ( MessageQueueException^ ) 
      {
         // Handle Message Queuing exceptions.
      }
      // Handle invalid serialization format.
      catch ( InvalidOperationException^ e ) 
      {
         Console::WriteLine( e->Message );
      }

      // Catch other exceptions as necessary.

      return;
   }
};

//**************************************************
// Provides an entry point into the application.
//		 
// This example sends and receives a message from
// a queue.
//**************************************************
int main()
{
   // Create a new instance of the class.
   MyNewQueue^ myNewQueue = gcnew MyNewQueue;

   // Send messages to a queue.
   myNewQueue->SendMessage( MessagePriority::Normal, "First Message Body." );
   myNewQueue->SendMessage( MessagePriority::Highest, "Second Message Body." );

   // Receive messages from a queue.
   myNewQueue->ReceiveMessage();
   myNewQueue->ReceiveMessage();

   return 0;
}
using System;
using System.Messaging;

namespace MyProject
{

    /// <summary>
    /// Provides a container class for the example.
    /// </summary>
    public class MyNewQueue
    {

        //**************************************************
        // Provides an entry point into the application.
        //		
        // This example sends and receives a message from
        // a queue.
        //**************************************************

        public static void Main()
        {
            // Create a new instance of the class.
            MyNewQueue myNewQueue = new MyNewQueue();

            // Send messages to a queue.
            myNewQueue.SendMessage(MessagePriority.Normal, "First Message Body.");
            myNewQueue.SendMessage(MessagePriority.Highest, "Second Message Body.");

            // Receive messages from a queue.
            myNewQueue.ReceiveMessage();
            myNewQueue.ReceiveMessage();

            return;
        }

        //**************************************************
        // Sends a string message to a queue.
        //**************************************************
        
        public void SendMessage(MessagePriority priority, string messageBody)
        {

            // Connect to a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Create a new message.
            Message myMessage = new Message();

            if(priority > MessagePriority.Normal)
            {
                myMessage.Body = "High Priority: " + messageBody;
            }
            else
            {
                myMessage.Body = messageBody;
            }

            // Set the priority of the message.
            myMessage.Priority = priority;

            // Send the Order to the queue.
            myQueue.Send(myMessage);

            return;
        }

        //**************************************************
        // Receives a message.
        //**************************************************
        
        public  void ReceiveMessage()
        {
            // Connect to the a queue on the local computer.
            MessageQueue myQueue = new MessageQueue(".\\myQueue");

            // Set the queue to read the priority. By default, it
            // is not read.
            myQueue.MessageReadPropertyFilter.Priority = true;

            // Set the formatter to indicate body contains a string.
            myQueue.Formatter = new XmlMessageFormatter(new Type[]
                {typeof(string)});
            
            try
            {
                // Receive and format the message.
                Message myMessage =	myQueue.Receive();

                // Display message information.
                Console.WriteLine("Priority: " +
                    myMessage.Priority.ToString());
                Console.WriteLine("Body: " +
                    myMessage.Body.ToString());
            }
            
            catch (MessageQueueException)
            {
                // Handle Message Queuing exceptions.
            }

            // Handle invalid serialization format.
            catch (InvalidOperationException e)
            {
                Console.WriteLine(e.Message);
            }
            
            // Catch other exceptions as necessary.

            return;
        }
    }
}
Imports System.Messaging


'Provides a container class for the example.
Public Class MyNewQueue
      
      

      ' Provides an entry point into the application.
      '		 
      ' This example sends and receives a message from
      ' a queue.

      Public Shared Sub Main()
         ' Create a new instance of the class.
         Dim myNewQueue As New MyNewQueue()
         
         ' Send messages to a queue.
         myNewQueue.SendMessage(MessagePriority.Normal, "First Message Body.")
         myNewQueue.SendMessage(MessagePriority.Highest, "Second Message Body.")
         
         ' Receive messages from a queue.
         myNewQueue.ReceiveMessage()
         myNewQueue.ReceiveMessage()
         
         Return
      End Sub
      
      
      

      ' Sends a string message to a queue.

      Public Sub SendMessage(priority As MessagePriority, messageBody As String)
         
         ' Connect to a queue on the local computer.
         Dim myQueue As New MessageQueue(".\myQueue")
         
         ' Create a new message.
         Dim myMessage As New Message()
         
         If priority > MessagePriority.Normal Then
            myMessage.Body = "High Priority: " + messageBody
         Else
            myMessage.Body = messageBody
         End If 
         ' Set the priority of the message.
         myMessage.Priority = priority
         
         
         ' Send the Order to the queue.
         myQueue.Send(myMessage)
         
         Return
      End Sub
      
      
      

      ' Receives a message.

      Public Sub ReceiveMessage()
         ' Connect to the a queue on the local computer.
         Dim myQueue As New MessageQueue(".\myQueue")
         
         ' Set the queue to read the priority. By default, it
         ' is not read.
         myQueue.MessageReadPropertyFilter.Priority = True
         
         ' Set the formatter to indicate body contains a string.
         myQueue.Formatter = New XmlMessageFormatter(New Type() {GetType(String)})
         
         Try
            ' Receive and format the message. 
            Dim myMessage As Message = myQueue.Receive()
            
            ' Display message information.
            Console.WriteLine(("Priority: " + myMessage.Priority.ToString()))
            Console.WriteLine(("Body: " + myMessage.Body.ToString()))
         
         
         
         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)
         End Try
         
         ' Catch other exceptions as necessary.
         Return
      End Sub
   End Class

Comentarios

BodyNormalmente, la propiedad del mensaje contiene los datos asociados al mensaje.The message's Body property usually contains the data associated with the message. Aunque también puede enviar datos específicos de la aplicación en las AppSpecific Extension propiedades y, debe incluir los datos del mensaje en el Body del mensaje siempre que sea posible.Although you can also send application-specific data in the AppSpecific and Extension properties, you should include message data in the Body of the message whenever possible. Solo Body se serializa o cifra el contenido de la propiedad.Only the Body property contents are serialized or encrypted.

La Body propiedad puede contener cualquier objeto cuyo tamaño no sea superior a 4 MB.The Body property can contain any object whose size does not exceed 4 MB. Si usa MessageQueue.Send para enviar cualquier objeto que no sea de tipo Message a MessageQueue , ese objeto se ubicará en la Body propiedad de la Message instancia devuelta por Peek o Receive .If you use MessageQueue.Send to send any object that is not of type Message to the MessageQueue, that object will be located in the Body property of the Message instance returned by Peek or Receive.

El argumento de cadena de MessageQueue.Send("hello.") es un ejemplo de este tipo de objeto genérico.The string argument in MessageQueue.Send("hello.") is an example of such a generic object.

La BodyType propiedad indica el tipo de información que se almacena en el cuerpo del mensaje.The BodyType property indicates the type of information that is stored in the message body. Message Queue Server usa esta información para identificar el tipo de contenido de la Body propiedad.Message Queuing uses this information to identify the type of the Body property contents.

Especifique la Body propiedad o la BodyStream propiedad antes de enviar el Message objeto.Specify either the Body property or the BodyStream property before sending the Message object. La Body propiedad puede ser cualquier objeto serializable, como una cadena de texto, un objeto de estructura, una instancia de clase o un objeto incrustado.The Body property can be any serializable object, such as a text string, structure object, class instance, or embedded object.

A menos que escriba el contenido del mensaje directamente en la BodyStream propiedad, establezca la Formatter propiedad antes de enviar el mensaje.Unless you write the contents of the message directly to the BodyStream property, set the Formatter property before you send the message. Cuando Send se llama al método en la MessageQueue instancia de, el cuerpo se serializa utilizando el formateador contenido en la Formatter propiedad.When the Send method is called on the MessageQueue instance, the body is serialized using the formatter contained in the Formatter property. Si envía el mensaje sin especificar un valor para la Formatter propiedad, el formateador tiene como valor predeterminado XmlMessageFormatter .If you send the message without specifying a value for the Formatter property, the formatter defaults to XmlMessageFormatter.

Nota

Si se intenta establecer el cuerpo de un mensaje en MaxValue , se producirá una OverflowException cuando se Send llame al método de la MessageQueue clase y ActiveXMessageFormatter se utilice.Attempting to set the body of a message to MaxValue will cause a OverflowException when the Send method of the MessageQueue class is called and the ActiveXMessageFormatter is used.

Se aplica a

Consulte también