/// <summary> /// send and wait reply /// </summary> /// <param name="sender"></param> /// <param name="msgLabel"></param> /// <param name="data"></param> /// <param name="timeout"></param> /// <returns></returns> public async static Task <object> SendAndWaitReply(AzureSBus sender, string msgLabel, dynamic data, int timeout = 60000) { var watch = new Stopwatch(); watch.Start(); // new a `result waiter` of MQReplyCenter to wait the result async var replyTo = F.NewGuid(); var waiter = MQReplyCenter.NewWaiter(replyTo); // sent the data to queue await sender.Send(msgLabel, data, replyToSessionId : MQReplyCenter.GetReplySessionId(), replyTo : replyTo); // get a waiter and waiting var task = waiter.wait(); if (await Task.WhenAny(task, Task.Delay(timeout)) == task) { Console.WriteLine("Queue Communication Elapsed : " + watch.Elapsed.TotalSeconds); return(task.Result); } else { //timeout MQReplyCenter.CancelWaiter(replyTo); var e = new MQReplyTimeoutException(sender.queueName, msgLabel, timeout); Console.ForegroundColor = System.ConsoleColor.DarkRed; Console.WriteLine(e); Console.ResetColor(); throw e; } }
public async static Task Send() { var data = new { msg = "hello", }; Console.WriteLine("cnt+1=" + Interlocked.Increment(ref cnt)); await sender.Send(MSG_LABEL, data); }
/// <summary> /// Send reply to a `session queue` /// </summary> /// <param name="toInstanceId">給哪個執行實體</param> /// <param name="toCallerId">給執行實體中的哪個呼叫者</param> /// <returns></returns> public async static Task SendReply(string toInstanceId, string toCallerId, dynamic data = null) { await sender.Send(MSG_LABEL, data, session : toInstanceId, replyTo : toCallerId); }