3.31. 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 solution creates query and result message queues if they do not exist. A message is sent to the query queue containing the query. The message is retrieved from the message queue and the query is parsed from it. The query is processed and the results are sent to the result queue. Finally the message is received from the result queue, cast to a DataSet
, and the result set is output to the console.
This project needs a reference to the System.Messaging
assembly.
The C# code in Program.cs in the project AsynchronousQueryMSMQ
is shown in Example 3-44.
Example 3-44. File: Program.cs for AsynchronousQueryMSMQ solution
using System; using System.Data; using System.Data.SqlClient; using System.Messaging; namespace AsynchronousQueryMSMQ { class Program { private static string queueNameQuery = @".\Private$\AdoNet35Cookbook_AsynchronousQuery"; private static string queueNameResult = @".\Private$\AdoNet35Cookbook_AsynchronousResult"; private static int contactID = 10; static void Main(string[] args) { // Create the query queue if it does not exist. if (!MessageQueue.Exists(queueNameQuery)) ...
Get ADO.NET 3.5 Cookbook, 2nd Edition 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.