- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Receiving Messages in .NET framework
Receiving Messages Make QR Code In .NET Framework Using Barcode encoder for .NET Control to generate, create QR-Code image in .NET applications. www.OnBarcode.comScanning QR In .NET Using Barcode reader for .NET framework Control to read, scan read, scan image in .NET framework applications. www.OnBarcode.comAn application can read the messages in a queue by using one of the following two techniques: the Receive method for synchronous retrieval of messages, or the ReceiveCompleted event of the MessageQueue object for asynchronous retrieval of messages. Retrieving a message synchronously is trivial: Make Barcode In Visual Studio .NET Using Barcode drawer for .NET Control to generate, create barcode image in Visual Studio .NET applications. www.OnBarcode.comScanning Barcode In .NET Framework Using Barcode scanner for Visual Studio .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.com Get a reference to the queue. (QUEUE_NAME has been defined previously.) Generate QR Code In C#.NET Using Barcode encoder for Visual Studio .NET Control to generate, create QR Code ISO/IEC18004 image in Visual Studio .NET applications. www.OnBarcode.comQR Code ISO/IEC18004 Creation In VS .NET Using Barcode creator for ASP.NET Control to generate, create QR image in ASP.NET applications. www.OnBarcode.comDim queue As New MessageQueue(QUEUE_NAME) Draw Quick Response Code In VB.NET Using Barcode drawer for .NET Control to generate, create QR Code JIS X 0510 image in Visual Studio .NET applications. www.OnBarcode.comCreate GTIN - 12 In .NET Using Barcode creation for .NET Control to generate, create GTIN - 12 image in Visual Studio .NET applications. www.OnBarcode.com Receive the next message, wait up to 10 seconds.
USS Code 39 Creation In Visual Studio .NET Using Barcode drawer for Visual Studio .NET Control to generate, create Code-39 image in Visual Studio .NET applications. www.OnBarcode.comEncoding EAN-13 In .NET Framework Using Barcode creation for .NET framework Control to generate, create EAN13 image in .NET applications. www.OnBarcode.comDim m As Message = queue.Receive(New TimeSpan(0, 0, 10)) Draw DataMatrix In .NET Using Barcode maker for .NET framework Control to generate, create ECC200 image in Visual Studio .NET applications. www.OnBarcode.comMSI Plessey Printer In Visual Studio .NET Using Barcode maker for .NET framework Control to generate, create MSI Plessey image in .NET applications. www.OnBarcode.com Read the body as a string.
Barcode Drawer In .NET Using Barcode printer for Reporting Service Control to generate, create bar code image in Reporting Service applications. www.OnBarcode.comGenerate EAN-13 Supplement 5 In None Using Barcode creator for Office Word Control to generate, create European Article Number 13 image in Word applications. www.OnBarcode.comDim targetTypes() As Type = {GetType(String)} Code-128 Generator In None Using Barcode generation for Online Control to generate, create Code 128 Code Set C image in Online applications. www.OnBarcode.comData Matrix 2d Barcode Generation In Java Using Barcode printer for Android Control to generate, create DataMatrix image in Android applications. www.OnBarcode.comm.Formatter = New XmlMessageFormatter(targetTypes) Code 128 Code Set A Decoder In Java Using Barcode scanner for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comGenerating EAN128 In Visual Studio .NET Using Barcode creator for Reporting Service Control to generate, create EAN 128 image in Reporting Service applications. www.OnBarcode.com Display the message body and label in TextBox controls.
Creating EAN 13 In Java Using Barcode maker for Android Control to generate, create EAN / UCC - 13 image in Android applications. www.OnBarcode.comMake Code 128 Code Set B In None Using Barcode drawer for Font Control to generate, create Code 128C image in Font applications. www.OnBarcode.comtxtLabel.Text = m.Label
txtMessage.Text = CStr(m.Body) Catch ex As Exception Often this is a MessageQueueException caused by a timeout. MsgBox(ex.Message, MsgBoxStyle.Critical) End Try The formatter being used to retrieve the body of the message BinaryMessageFormatter or XmlMessageFormatter must match the formatter applied when sending the message. 31: Serviced Components
The constructors of these two formatter classes take an array of System.Type objects that represent the types expected in the message s body. (In the previous example, we expect only strings.) The receiver application can discern among different types using the TypeOf operator, as in the following: Dim targetTypes() As Type = {GetType(String), GetType(DataSet)} m.Formatter = New XmlMessageFormatter(targetTypes) Dim body As Object = m.Body
If TypeOf body Is String Then
txtMessage.Text = CStr(body) Else Display the DataSet as XML. txtMessage.Text = CType(body, DataSet).GetXml End If The Receive method extracts the message from the queue, and you can t undo its action. Alternatively, you can use the Peek method to get the next message in the queue while leaving it in the queue: Peek the next message, wait up to 5 seconds. Dim m As Message = queue.Peek(New TimeSpan(0, 0, 5)) You aren t limited to reading messages one at a time. If you expect to find several mes sages in the queue, you can collect all of them more efficiently in one operation by means of the GetAllMessages method: Dim messages() As Message = queue.GetAllMessages() Retrieving messages in an asynchronous fashion requires that you set up a handler for the ReceiveCompleted event of a MessageQueue object by using either a WithEvents variable or the AddHandler keyword and then start listening for events by means of the BeginReceive method: Dim queue As New MessageQueue(Form1.QUEUE_NAME) AddHandler queue.ReceiveCompleted, AddressOf NewMessage
Start listening to events.
queue.BeginReceive() The handler for the event can access the incoming message by using one of the prop erties of the ReceiveCompletedEventArgs object passed to its second argument: Private Sub NewMessage(ByVal sender As Object, ByVal e As ReceiveCompletedEventArgs) Dim m As Message = e.Message Dim targetTypes() As Type = {GetType(String)} m.Formatter = New XmlMessageFormatter(targetTypes) Dim body As String = CStr(m.Body) Just display body and label in this demo program. MsgBox(body, MsgBoxStyle.Information, m.Label) Wait for the next message. queue.BeginReceive() End Sub Part VII: Advanced Topics
As the previous routine illustrates, the code in the event handler must call the BeginReceive method to receive subsequent events. (The MessageQueue object also exposes a PeekCompleted event, which you activate with a BeginPeek method.) Transactional Queues
MSMQ supports transactional queues, which can take part in two types of transactions: internal transactions and external transactions. Internal transactions are managed directly by MSMQ, are more efficient, and have a simple programming model. External transactions require the presence of MS DTC (or another transaction coordinator), add more overhead, and are more complex. Only external transactions can involve other types of resources, such as databases. (For information about external transactions, read the MSDN documentation.) Regardless of the transition type, MSMQ can guarantee either that all the messages in the transaction are sent (or received), or that none of them is sent (or received). Additionally, MSMQ guarantees that all the messages inside a transaction are received in the same order in which they were sent. The following example shows how you can create a transac tional queue, send a group of messages, and then commit or roll back the transaction:
|
|