public void ToServiceBusMessage_Converts_All_Information() { var queueTMessage = new QueueTMessage { Id = "123", ContentType = "contentType", Properties = new Dictionary <string, string> { { "prop1", "value1" } }, EncodedBody = new byte[] { 1, 2, 3 }, Created = DateTime.UtcNow, MessageType = "messageType" }; var sbMessage = queueTMessage.ToServiceBusMessage(); Assert.Equal(queueTMessage.Id, sbMessage.MessageId); Assert.Equal(queueTMessage.ContentType, sbMessage.ContentType); Assert.Equal(queueTMessage.EncodedBody, sbMessage.Body); Assert.Equal( new Dictionary <string, object> { { "prop1", "value1" }, { ServiceBusBroker.MessageTypeProperty, queueTMessage.MessageType } }, sbMessage.UserProperties); }
public async Task ProcessMessageAsync(QueueTMessage message, CancellationToken cancellationToken) { try { foreach (var handlerType in _options.MessageHandlerTypes) { using (var scope = _serviceProvider.CreateScope()) { var messageHandler = ActivatorUtilities.CreateInstance(scope.ServiceProvider, handlerType) as IMessageHandler; if (!messageHandler.CanHandleMessage(message)) { continue; } await messageHandler.HandleMessage(message); return; } } } catch (Exception ex) { if (ex is MessageProcessingException) { throw; } _logger.LogError(ex, "Application Exception while proccessing message"); throw new MessageProcessingException(innerException: ex); } // There are no handlers for this message throw new MessageProcessingException(MessageAction.Acknowledge, $"No plugins registered that may handle this message"); }
public async Task HandleMessage(QueueTMessage message) { if (!message.Properties.TryGetValue(NotificationMessage.TopicPropertyKey, out var topic)) { throw new MessageProcessingException(message: $"Topic name not present"); } var notificationDefinition = _notificationRegistry.GetNotificationByTopic(topic); var subscriptions = _notificationRegistry.GetSubscriptions(notificationDefinition); if (0 == subscriptions.Length) { return; } JObject argument = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(message.EncodedBody)) as JObject; var properties = argument.Properties().ToArray(); foreach (var subscription in subscriptions) { object taskArgument = null; var options = new DispatchOptions { Queue = subscription.TaskQueue }; taskArgument = new Dictionary <string, object> { { subscription.MessageParameter.Name, argument } }; await _taskService.DelayAsync(subscription.Method, taskArgument, options); } }
public async Task SendAsync(string queueName, QueueTMessage message) { var queueClient = GetOrCreateQueueClient(queueName); var serviceBusMessage = message.ToServiceBusMessage(); await queueClient.SendAsync(serviceBusMessage); }
public async Task HandleMessage(QueueTMessage message) { if (!message.Properties.TryGetValue(TaskNamePropertyKey, out var taskName)) { throw new ArgumentException("TaskName not present in message"); } var definition = GetTaskDefinition(taskName); var sw = new Stopwatch(); sw.Start(); var arguments = new Dictionary <string, object>(); try { var jsonArguments = JsonConvert.DeserializeObject(Encoding.UTF8.GetString(message.EncodedBody)) as JObject; foreach (var property in jsonArguments.Properties()) { var parameter = definition.Parameters.FirstOrDefault(p => p.Name.Equals(property.Name)); if (null == parameter) { continue; } arguments[parameter.Name] = property.Value.ToObject(parameter.ParameterType); } }catch (Exception ex) { _logger.LogCritical("Unable to deserialize task: TaskName={TaskName} Exception={exception}", taskName, ex.Message); return; } var taskMessage = new TaskMessage { Name = taskName, Arguments = arguments }; _logger.LogDebug("TaskMessage Deserialized: TaskName={TaskName} TotalMilliseconds={TotalMilliseconds} MessageSize={MessageSize}", taskMessage.Name, sw.Elapsed.TotalMilliseconds, message.EncodedBody.Length); try { _logger.LogInformation("Task Starting: TaskName={TaskName}", taskMessage.Name); sw.Restart(); await ExecuteTaskMessageAsync(taskMessage); _logger.LogInformation("Task Completed: TaskName={TaskName} TotalMilliseconds={TotalMilliseconds}", taskMessage.Name, sw.Elapsed.TotalMilliseconds); }catch (Exception ex) { _logger.LogCritical("Task Failed: TaskName={TaskName} TotalMilliseconds={TotalMilliseconds} Exception={ex.Message}", taskMessage.Name, sw.Elapsed.TotalMilliseconds, ex.Message); // We can add some certaim properties to the message // This will allow us to implement certain features like // Task Retry // Dead Lettering } }
public async Task SendAsync(string queueName, QueueTMessage message) { if (!Queues.TryGetValue(queueName, out var queue)) { queue = new Queue <QueueTMessage>(); Queues[queueName] = queue; } queue.Enqueue(message); await Task.CompletedTask; }
public static Message ToServiceBusMessage(this QueueTMessage message) { var serviceBusMessage = new Message(message.EncodedBody) { MessageId = message.Id, ContentType = message.ContentType }; foreach (var property in message.Properties) { serviceBusMessage.UserProperties[property.Key] = property.Value; } serviceBusMessage.UserProperties[ServiceBusBroker.MessageTypeProperty] = message.MessageType; return(serviceBusMessage); }
public static QueueTMessage ToQueueTMessage(this Message message) { var properties = new Dictionary <string, string>(); var queueTMessage = new QueueTMessage { Id = message.MessageId, ContentType = message.ContentType, EncodedBody = message.Body, Properties = new Dictionary <string, string>() }; queueTMessage.MessageType = message.UserProperties[ServiceBusBroker.MessageTypeProperty] as string; message.UserProperties.Remove(ServiceBusBroker.MessageTypeProperty); foreach (var property in message.UserProperties) { if (message.UserProperties[property.Key] is string value) { queueTMessage.Properties[property.Key] = value; } } return(queueTMessage); }
public bool CanHandleMessage(QueueTMessage message) => string.Equals(message?.MessageType, MessageType, StringComparison.InvariantCultureIgnoreCase);
public bool CanHandleMessage(QueueTMessage message) => message.MessageType == NotificationMessage.MessageType;