private async Task ProcessingTaskAsync(byte[] data) { var oldContext = Core.ContextGroupName; try { (var body, var correlationId) = NATSQueueClient.GetFromMessageBody(data); Counters.IncrementTotalReceivingBytes(body.Count); Core.Log.LibVerbose("Received {0} bytes from the Queue '{1}/{2}'", body.Count, Connection.Route, Connection.Name); var messageBody = ReceiverSerializer.Deserialize(body, _messageType); switch (messageBody) { case RequestMessage request when request.Header != null: if (!string.IsNullOrEmpty(request.Header.ContextGroupName)) { Core.ContextGroupName = request.Header.ContextGroupName; } request.Header.ApplicationReceivedTime = Core.Now; Counters.IncrementReceivingTime(request.Header.TotalTime); if (request.Header.ClientName != Config.Name) { Core.Log.Warning("The Message Client Name '{0}' is different from the Server Name '{1}'", request.Header.ClientName, Config.Name); } var evArgs = new RequestReceivedEventArgs(_name, Connection, request, body.Count, SenderSerializer, Config.RequestOptions.ServerReceiverOptions.CancellationBeforeClientResponseTimeoutInSec); if (request.Header.ResponseQueue != null) { evArgs.ResponseQueues.Add(request.Header.ResponseQueue); } await OnRequestReceivedAsync(evArgs).ConfigureAwait(false); break; case ResponseMessage response when response.Header != null: if (!string.IsNullOrEmpty(response.Header.Request.Header.ContextGroupName)) { Core.ContextGroupName = response.Header.Request.Header.ContextGroupName; } response.Header.Response.ApplicationReceivedTime = Core.Now; Counters.IncrementReceivingTime(response.Header.Response.TotalTime); var evArgs2 = new ResponseReceivedEventArgs(_name, response, body.Count); await OnResponseReceivedAsync(evArgs2).ConfigureAwait(false); break; } Counters.IncrementTotalMessagesProccesed(); } catch (Exception ex) { Counters.IncrementTotalExceptions(); Core.Log.Write(ex); Interlocked.Exchange(ref _exceptionSleep, 1); } finally { Core.ContextGroupName = oldContext; } }
private void MessageHandler(object sender, MsgHandlerEventArgs e) { Core.Log.LibVerbose("Message received"); try { (var body, var correlationId) = NATSQueueClient.GetFromMessageBody(e.Message.Data); var rMsg = new NATSQMessage { CorrelationId = correlationId, Body = body }; Task.Run(() => EnqueueMessageToProcessAsync(ProcessingTaskAsync, rMsg)); } catch (Exception ex) { Core.Log.Write(ex); } }