2.22. Querying Data Asynchronously with Message Queuing
Problem
You want to asynchronously retrieve data from a system that is not always connected.
Solution
You must:
Use message queuing to construct and send a data request from the client.
Access and process the requesting message at the server.
Compose and send a response message containing the result set to the client.
Retrieve the response at the client and deserialize it into a
DataSet
.
The sample code contains three event handlers:
- Send
Button.Click
Checks if the
query
message queue exists and creates it if necessary. AMessageQueue
object is created to access the queue. A message is sent to the queue containing theCustomerID
which the user wants information about.- Process Query
Button.Click
Checks if the
query
message queue exists and creates it if necessary. AMessageQueue
object is created to access the queue. An attempt is made to receive a message from the queue, waiting one second before giving up. If a message is received, theCustomerID
is extracted from the message and the message queue is closed. ADataSet
is created and aDataAdapter
is used to return the record for the requestedCustomerID
into a CustomerDataTable
in theDataSet
. A result queue is created if necessary and a message labeled with theCustomerID
and containing theDataSet
with the asynchronous query results is sent to the queue.- Process Result
Button.Click
Checks if the
result
message queue exists and creates it if necessary. AMessageQueue
object is created ...
Get ADO.NET Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.