public void WriteMessage(Inputfile message) { // Now, suppose you would like to check if the queue exists and if it does, // send a message to it. If the queue doesn't exist, you may want to create // a new one and then send it a message. This is exactly what the following // code listing does for you. try { if (MessageQueue.Exists(path)) { messageQueue = new MessageQueue(path); messageQueue.Label = description; } else { MessageQueue.Create(path); messageQueue = new MessageQueue(path); messageQueue.Label = description; } messageQueue.Send(message); } catch { throw; } finally { messageQueue.Dispose(); } }
/// <summary> /// The following code listing illustrates how you can process the messages stored in a message queue using C#. /// </summary> /// <returns>A list of messages</returns> public Inputfile ReadMessage() { Inputfile inputfile = new Inputfile(); MessageQueue messageQueue = new MessageQueue(path); messageQueue.Formatter = new XmlMessageFormatter(new Type[] { typeof(Inputfile) }); try { Message message = messageQueue.Receive(new TimeSpan(0, 0, 1)); inputfile = (Inputfile)message.Body; } catch (MessageQueueException e) { // Handle Message Queuing exceptions. if (e.Message == "Timeout for the requested operation has expired.") { throw new Exception("Queue is empty"); } else { throw(e); } } catch (InvalidOperationException e) { // Handle invalid serialization format. throw (e); } return(inputfile); }
public void WriteMessages(string[] messages) { foreach (string message in messages) { Inputfile inputfile = new Inputfile(); inputfile.File = message; try { WriteMessage(inputfile); } catch (Exception ex) { throw new Exception(ex.Message); } } }