SendMSMQMessage
The SendMSMQMessage sample demonstrates how to send a message to an MSMQ port from a .NET-based application. It also provides instructions about how to configure Microsoft BizTalk Server to use an MSMQ receive location.
You should be aware that many operations in Message Queuing are asynchronous. That is, many MSMQ API calls (for example, System.Messaging.MessageQueue.Send) return to the caller before the requested operation has fully completed. MSMQ provides a mechanism to deliver feedback to the application after the operation has completed. This mechanism involves the use of an "Admin Queue." MSMQ returns feedback in the form of a message in the Admin Queue. The Admin Queue to which MSMQ will return feedback is specified when the original MSMQ API call is made. So, for example, when sending a message using the System.Messaging.MessageQueue.Send API, the application can specify the name of an Admin Queue by using the PROPID_M_ADMIN_QUEUE message property on the message passed in the call to System.Messaging.MessageQueue.Send. Even though the application may get a successful return code on the System.Messaging.MessageQueue.Send call, if the message send operation subsequently fails, MSMQ writes a message to that effect to the specified Admin Queue. If the application does not specify an Admin Queue, the send failure results in the message being lost and no diagnostics captured — in effect, the message disappears without any evidence. There are a number of error situations in MSMQ that can cause this to happen, for example, doing a non-transactional send to a transactional queue.
In the context of this sample, it is important that the code specify a transaction type in the call to System.Messaging.MessageQueue.Send that is consistent with the transaction support specified for the queue to which the message is sent. If this is not done and if no Admin Queue is specified (as is the case in this sample), then MSMQ discards the sent message with no indication that it has done so (that is, no error code returned to the application, no diagnostics written to the event log, and so on).
<Samples Path>\AdaptersUsage\SendMSMQMessage\
The following table shows the files in this sample and describes their purpose.
Files | Description |
---|---|
App.ico, AssemblyInfo.cs, SendMSMQMessage.csproj, SendMSMQMessage.sln | Provide project, solution, and related files for the simple graphical application for this sample. |
Form1.cs, Form1.resx | Provide Microsoft Visual C#.NET source and form files for the simple graphical application for this sample. |
You can use the code in the simple graphical application included with this sample as an example of how to send messages to MSMQ receive locations within BizTalk Server from .NET-enabled applications such as Microsoft Office, from ASP.NET pages, and so on.
Using Visual Studio, open the solution file SendMSMQMessage.sln.
On the Build menu, click Build Solution.
Use the following procedures to configure BizTalk Server and create the MSMQ queue for running the sample.
Click Start, right-click Computer, and then click Manage.
Expand the Features node. If Message Queuing is not installed, right-click Features and select Add Features. Check Message Queuing, click Next, and then click Install to install MSMQ on that system.
Expand the Message Queuing node.
Right-click the Private Queues node, click New, and then click Private Queue.
Under Queue name, enter
test
. Ensure that the Transactional check box is selected.Click OK.
Click Start, right-click Computer, and then click Manage.
Expand Services and Applications, and then expand the Message Queuing node.
Note
If Message Queuing is not installed in the computer, go to Control Panel > Programs > Programs and Features, and then select Turn Windows features on or off. Check all the features under Microsoft Message Queue (MSMQ) Server, and then click OK.
Right-click the Private Queues node, click New, and then click Private Queue.
Under Queue name, enter test. Ensure that the Transactional check box is selected.
Click OK.
Select a folder in which to receive messages. The following steps assume that you have selected C:\Demo\Report, but you can adjust the steps as necessary for another folder.
Open the BizTalk Server Administration console.
Create a new application named MSMQSample.
Right-click Receive Ports, click New, and then click One-way Receive Port.
In the Receive Port Properties dialog box, in the Name box enter MyReceivePort, and then click OK.
Right-click Receive Locations, click New, and then click One-way Receive Location. In the Select a Receive Port dialog box, select the receive port you just created and click OK.
In the Receive Location Properties dialog box, in the Name box, type a name for the receive port, such as MSMQReceiveLocation.
In the Receive Location Properties dialog box, for the transport type, select MSMQ .
Click Configure to open the MSMQ Transport Properties dialog box. Set Queue to
localhost\private$\test
, set Transactional toTrue
, and then click OK.Expand the application, select Send Ports, select New, select Static One-way Send Port.
In the Send Port Properties dialog box, in the Name box, type a name for the send port, such as MySendPort.
Set the Transport Type property to FILE.
Click Configure to open the File Transport Properties dialog box.
In the FILE Transport Properties dialog box, set the Destination Folder property to C:\Demo\Report, keep the default settings for the other properties, and then click OK.
Select Filters, and then add a new row by setting Property to BTS.ReceivePortName. Leave the Operator column set to ==, set the Value column to MyReceivePort, and then click OK.
Right-click your new send port, and then click Enlist.
Right-click your new send port again, and then click Start.
Right-click your new receive location, and then click Enable.
BizTalk Server is now ready to work with this sample.
Use the following procedure to run the SendMSMQMessage sample.
In a command window, navigate to the following folder:
<Samples Path>\AdaptersUsage\SendMSMQMessage\bin\Debug
Run the file SendMSMQMessage.exe, which starts the graphical application that provides the user interface for this sample.
In the graphical application, in the BizTalk machine name box, type the name of the local computer.
Try the Send Wrapped option:
Optionally change the text in the Message body box.
Click Send wrapped.
If the operation succeeds, you will see the following:
The word Success appears in red font in the box immediately above the buttons.
A file appears in your destination folder, C:\Demo\Reports. This file contains the text from the Message body box wrapped in simple XML tags by the .NET message queuing library.
If the operation fails, you will see an error message in the box immediately above the buttons.
Try the Send Exact option:
Optionally change the text in the Message body box.
Click Send exact.
If the operation succeeds, you will see the following:
The word Success appears in red font in the box immediately above the buttons.
A file appears in your destination folder, C:\Demo\Reports. This file contains the text from the Message body box exactly as it appears in the text box.
If the operation fails, you will see an error message in the box immediately above the buttons.