Compartir a través de


Visual Basic Code Example: Requesting Response Messages

 

Applies To: Windows 10, Windows 7, Windows 8, Windows 8.1, Windows Server 2008, Windows Server 2008 R2, Windows Server 2012, Windows Server 2012 R2, Windows Server Technical Preview, Windows Vista

This example provides a Sub procedure that requests response messages based on a given response queue. This procedure sets the MSMQMessage.ResponseDestination property of the message, and then sends the message to a known destination queue.

For information on responding to sent messages, see Response Messages.

This example uses the MSMQMessage.ResponseDestination property (introduced in MSMQ 3.0) to specify the response queue. MSMQMessage.ResponseDestination accepts an MSMQDestination object that can reference the following format name types.

  • A single public, private, or direct format name. These format names are used to specify a single response queue.

    For information about these format names, see Public Format Names, Private Format Names, and Direct Format Names.

  • A multiple-element format name. This format name is used to specify multiple response queues.

    For information about multiple-element format names, see Multiple-Element Format Names.

  • A distribution list format name. This format name is used to specify multiple response queues.

    For information about distribution list format names, see Distribution List Format Names.

  • A multicast address format name. This format name is used to specify multiple response queues.

    For information about multicast address format names, see Multicast Address Format Names.

To request response messages

  1. Declare the objects needed to send the message that requests tracing. This procedure declares the following objects.

    Two MSMQDestination objects (for the destination queue and the response queue)

    MSMQMessage (for a message that requests response messages)

  2. Generate a direct format name for the destination queue and the response queue using the computer and queue names supplied by the caller.

  3. Set the MSMQDestination.FormatName property for the destination and response queue objects.

  4. Set the MSMQMessage.ResponseDestination property using the response queue destination object.

  5. Optional. Set additional message properties. This procedure sets the MSMQMessage.Label property of the message to "Test Message: Response".

  6. Call MSMQMessage.Send to send a copy of the message to the destination queue referenced by the format name. Note that when using an MSMQDestination object, Message Queuing implicitly opens the destination queue with send access and then closes the destination queue when the object is released.

Code Example

The following code example requires MSMQ 3.0.

Sub RequestResponse( _  
                    strComputerName As String, _  
                    strDestQueueName As String, _  
                    strRespQueueName As String _  
                    )  
  Dim destOrig As New MSMQDestination  
  Dim destResp As New MSMQDestination  
  Dim msg As New MSMQMessage  
  
  ' Create format names of the destination and response queues.  
  strDestFormatName = "DIRECT=OS:" & strComputerName & "\" & strDestQueueName  
  strRespFormatName = "DIRECT=OS:" & strComputerName & "\" & strRespQueueName  
  
  ' Set format name of MSMQDestination object for destination  
  ' queue and response queue.  
  On Error GoTo ErrorHandler  
  destOrig.FormatName = strDestFormatName  
  destResp.FormatName = strRespFormatName  
  
  ' Set MSMQMessage.ResponseDestination to specify the response queue.  
  Set msg.ResponseDestination = destResp  
  
  ' Set other message properties.  
  msg.Label = "Test Message: Response"  
  
  ' Send the message.  
  msg.Send DestinationQueue:=destOrig  
  
  Exit Sub  
  
ErrorHandler:  
  MsgBox "Error " + Hex(Err.Number) + " was returned." _  
         + Chr(13) + Err.Description  
End Sub