private static void Main() { //var factory = new ConnectionFactory() { HostName = "localhost" }; //using (var connection = factory.CreateConnection()) //using (var channel = connection.CreateModel()) //{ // channel.QueueDeclare(queue: "hello", durable: false, exclusive: false, autoDelete: false, arguments: null); // var consumer = new EventingBasicConsumer(channel); // consumer.Received += (model, ea) => // { // var body = ea.Body; // var message = Encoding.UTF8.GetString(body); // Console.WriteLine(" [x] Received {0}", message); // }; // channel.BasicConsume(queue: "hello", noAck: true, consumer: consumer); // Console.WriteLine(" Press [enter] to exit."); // Console.ReadLine(); //} //while (true) //{ try { var queue = new MessageQueue(@".\Private$\HelloWorld") { Formatter = new XmlMessageFormatter(new[] { "".GetType() }) }; queue.ReceiveCompleted += (sender, e) => { var msg = queue.EndReceive(e.AsyncResult); Console.WriteLine(msg.Body.ToString()); queue.BeginReceive(); }; queue.BeginReceive(); //var message = queue.Receive(new TimeSpan(0, 0, 1)); //message.Formatter = new XmlMessageFormatter( // new String[] { "System.String, mscorlib" }); //Console.WriteLine(message.Body.ToString()); } catch (Exception ex) { Console.WriteLine("No Message"); } //Thread.Sleep(100); //} Console.WriteLine("Listening... Press any key to quit"); Console.ReadLine(); }
private async Task MyReceiveCompleted(Object source, ReceiveCompletedEventArgs asyncResult, string channelToReturnTo, string path) { ResultModel resultModel = new ResultModel(); try { // Connect to the queue. System.Messaging.MessageQueue mq = (System.Messaging.MessageQueue)source; // End the asynchronous Receive operation. System.Messaging.Message myMessage = mq.EndReceive(asyncResult.AsyncResult); resultModel.message = MessageConvert.ConvertFromSystemMessage(myMessage); resultModel.Result = (int)ResultsEnum.Done; // Restart the asynchronous Receive operation. Response response = await initiator.SendRequestAsync(new KubeMQ.SDK.csharp.CommandQuery.LowLevel.Request() { Metadata = "RecieveOK", Body = Converter.ToByteArray(resultModel), CacheKey = "", CacheTTL = 0, Channel = channelToReturnTo, ClientID = clientID, RequestType = RequestType.Query, Timeout = _timeout }); } catch (Exception ex) { _logger.LogCritical(string.Format("Failed to peek path {0} on ex {1}", channelToReturnTo, ex.Message)); } }
public T EndReceive <T>(IAsyncResult result) { var message = m_Queue.EndReceive(result); var body = GetBody <T>(message); return(body); }
void replyQueue_ReceiveCompleted(object sender, ReceiveCompletedEventArgs e) { replyQueue = (MessageQueue)sender; Message replyMessage = replyQueue.EndReceive(e.AsyncResult); Console.WriteLine("Received reply"); Console.WriteLine("\tTime: {0}", DateTime.Now.ToString("HH:mm:ss.ffffff")); Console.WriteLine("\tMessage ID: {0}", replyMessage.Id); Console.WriteLine("\tCorrel: {0}", replyMessage.CorrelationId); Console.WriteLine("\tReply To : {0}", "<n/a>"); Console.WriteLine("\tContents: {0}", replyMessage.Body.ToString()); replyQueue.BeginReceive(); }
private static Message Receive(MessageQueue queue, CancellationToken cancelToken) { var receiveAsyncResult = queue.BeginReceive(); WaitHandle.WaitAny(new[] { receiveAsyncResult.AsyncWaitHandle, cancelToken.WaitHandle }); Message message = null; if (!cancelToken.IsCancellationRequested) { message = queue.EndReceive(receiveAsyncResult); } return message; }
private void qOrders_ReceiveCompleted(object sender, System.Messaging.ReceiveCompletedEventArgs e) { // This event fires when a message is received. try { // Get the message Message m; m = qOrders.EndReceive(e.AsyncResult); // Cast the message body to an object instance Server.MSMQOrders o; o = (Server.MSMQOrders)m.Body; // if we did the following line of code: // o.Process() // our listening thread would be blocked until // it finished. Instead will use the built-in // CLR Thread Pool. // Note that our MSMQOrders.Process method has to match // expected signature defined by the QueueUserWorkItem method. // See the documenation for more information. ThreadPool.QueueUserWorkItem(new WaitCallback(o.Process)); // Now continue listening for messages this.qOrders.BeginReceive(); } catch (MessageQueueException exp) { this.EventLog.WriteEntry(exp.Message); } catch (Exception exp) { this.EventLog.WriteEntry(exp.Message); } }