Message Constructor (Object, IMessageFormatter)

 

Initializes a new instance of the Message class using the specified formatter to serialize the specified object into the body of the message.

Namespace:   System.Messaging
Assembly:  System.Messaging (in System.Messaging.dll)

Public Sub New (
	body As Object,
	formatter As IMessageFormatter
)

Parameters

body
Type: System.Object

The object to be serialized into the body of the message.

formatter
Type: System.Messaging.IMessageFormatter

A IMessageFormatter that specifies the formatter with which to serialize the message body.

Use this overload to create a new instance of the Message class that contains the Body specified by the body parameter and that uses any valid formatter to serialize the body. The body parameter is any object that can be serialized, such as a text string, a structure object, a class instance, or an embedded object. If you change the Body or Formatter property at any time before calling Send, the message will be serialized according to the new property value.

The XmlMessageFormatter is loosely coupled, so it is not necessary to have the same object type on the sender and receiver when using this format. The ActiveXMessageFormatter and BinaryMessageFormatter serialize the data into binary representation. The ActiveXMessageFormatter is used when sending or receiving COM components.

The following table shows initial property values for an instance of Message.

Property

Initial value

AcknowledgeType

AcknowledgeType.None

AdministrationQueue

null

AppSpecific

0

AttachSenderId

true

AuthenticationProviderName

Microsoft Base Cryptographic Provider version 1.0

AuthenticationProviderType

CryptoProviderType.RSA_FULL

Body

The body parameter.

BodyStream

Stream.null

BodyType

0

ConnectorType

Guid.Empty

CorrelationId

An empty string ("")

DestinationSymmetricKey

A zero-length array of bytes

DigitalSignature

A zero-length array of bytes

EncryptionAlgorithm

EncryptionAlgorithm.RC2

Extension

A zero-length array of bytes

Formatter

The formatter parameter.

HashAlgorithm

HashAlgorithm.MD5

Label

An empty string ("")

Priority

MessagePriority.Normal

Recoverable

false

ResponseQueue

null

SenderCertificate

A zero-length array of bytes

TimeToBeReceived

Message.InfiniteTimeout

TimeToReachQueue

Message.InfiniteTimeout

TransactionStatusQueue

null

UseAuthentication

false

UseDeadLetterQueue

false

UseEncryption

false

UseJournalQueue

false

UseTracing

false

Imports System
Imports System.Messaging
Imports System.Drawing
Imports System.IO


Namespace MyProj
    _


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

         ' Create a queue on the local computer.
         CreateQueue(".\myQueue")

         ' Send a message to a queue.
         myNewQueue.SendMessage()

         ' Receive a message from a queue.
         myNewQueue.ReceiveMessage()

         Return
      End Sub 'Main


      '**************************************************
      ' Creates a new queue.
      '**************************************************
      Public Shared Sub CreateQueue(queuePath As String)
         Try
            If Not MessageQueue.Exists(queuePath) Then
               MessageQueue.Create(queuePath)
            Else
               Console.WriteLine((queuePath + " already exists."))
            End If
         Catch e As MessageQueueException
            Console.WriteLine(e.Message)
         End Try
      End Sub 'CreateQueue


      '**************************************************
      ' Sends an image to a queue, using the BinaryMessageFormatter.
      '**************************************************
      Public Sub SendMessage()
         Try

            ' Create a new bitmap.
            ' The file must be in the \bin\debug or \bin\retail folder, or
            ' you must give a full path to its location.
            Dim myImage As Image = Bitmap.FromFile("SentImage.bmp")

            ' Connect to a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")

            Dim myMessage As New Message(myImage, New BinaryMessageFormatter())

            ' Send the image to the queue.
            myQueue.Send(myMessage)
         Catch e As ArgumentException
            Console.WriteLine(e.Message)
         End Try 

         Return
      End Sub 'SendMessage



      '**************************************************
      ' Receives a message containing an image.
      '**************************************************
      Public Sub ReceiveMessage()

         Try

            ' Connect to the a queue on the local computer.
            Dim myQueue As New MessageQueue(".\myQueue")

            ' Set the formatter to indicate body contains an Order.
            myQueue.Formatter = New BinaryMessageFormatter()

            ' Receive and format the message. 
            Dim myMessage As System.Messaging.Message = myQueue.Receive()
            Dim myImage As Bitmap = CType(myMessage.Body, Bitmap)

            ' This will be saved in the \bin\debug or \bin\retail folder.
            myImage.Save("ReceivedImage.bmp", System.Drawing.Imaging.ImageFormat.Bmp)



         'CatchCatch
         ' Handle Message Queuing exceptions.

         ' Handle invalid serialization format.
         Catch e As InvalidOperationException
            Console.WriteLine(e.Message)

         Catch e As IOException
         End Try
         ' Handle file access exceptions.

         ' Catch other exceptions as necessary.
         Return
      End Sub 'ReceiveMessage
   End Class 'MyNewQueue
End Namespace 'MyProj

.NET Framework
Available since 1.1
Return to top
Show: