public virtual void Publish <TCommand>(IEnumerable <TCommand> commands) where TCommand : ICommand <TAuthenticationToken> { IList <TCommand> sourceCommands = commands.ToList(); IList <string> sourceCommandMessages = new List <string>(); IList <EventData> brokeredMessages = new List <EventData>(sourceCommands.Count); foreach (TCommand command in sourceCommands) { if (!AzureBusHelper.PrepareAndValidateCommand(command, "Azure-EventHub")) { continue; } var brokeredMessage = new EventData(Encoding.UTF8.GetBytes(MessageSerialiser.SerialiseCommand(command))); brokeredMessage.Properties.Add("Type", command.GetType().FullName); brokeredMessages.Add(brokeredMessage); sourceCommandMessages.Add(string.Format("A command was sent of type {0}.", command.GetType().FullName)); } EventHubPublisher.SendBatch(brokeredMessages); foreach (string message in sourceCommandMessages) { Logger.LogInfo(message); } }
public virtual void Publish <TEvent>(IEnumerable <TEvent> events) where TEvent : IEvent <TAuthenticationToken> { IList <TEvent> sourceEvents = events.ToList(); IList <string> sourceEventMessages = new List <string>(); IList <Microsoft.ServiceBus.Messaging.EventData> brokeredMessages = new List <Microsoft.ServiceBus.Messaging.EventData>(sourceEvents.Count); foreach (TEvent @event in sourceEvents) { if (!AzureBusHelper.PrepareAndValidateEvent(@event, "Azure-EventHub")) { continue; } var brokeredMessage = new Microsoft.ServiceBus.Messaging.EventData(Encoding.UTF8.GetBytes(MessageSerialiser.SerialiseEvent(@event))); brokeredMessage.Properties.Add("Type", @event.GetType().FullName); brokeredMessages.Add(brokeredMessage); sourceEventMessages.Add(string.Format("A command was sent of type {0}.", @event.GetType().FullName)); } try { EventHubPublisher.SendBatch(brokeredMessages); } catch (QuotaExceededException exception) { Logger.LogError("The size of the event being sent was too large.", exception: exception, metaData: new Dictionary <string, object> { { "Events", sourceEvents } }); throw; } catch (Exception exception) { Logger.LogError("An issue occurred while trying to publish a event.", exception: exception, metaData: new Dictionary <string, object> { { "Events", sourceEvents } }); throw; } foreach (string message in sourceEventMessages) { Logger.LogInfo(message); } }
public virtual void Publish <TEvent>(IEnumerable <TEvent> events) where TEvent : IEvent <TAuthenticationToken> { IList <TEvent> sourceEvents = events.ToList(); DateTimeOffset startedAt = DateTimeOffset.UtcNow; Stopwatch mainStopWatch = Stopwatch.StartNew(); string responseCode = "200"; bool wasSuccessfull = false; IDictionary <string, string> telemetryProperties = new Dictionary <string, string> { { "Type", "Azure/EventHub" } }; string telemetryName = "Events"; string telemetryNames = string.Empty; foreach (TEvent @event in sourceEvents) { string subTelemetryName = string.Format("{0}/{1}", @event.GetType().FullName, @event.Id); var telemeteredEvent = @event as ITelemeteredMessage; if (telemeteredEvent != null) { subTelemetryName = telemeteredEvent.TelemetryName; } telemetryNames = string.Format("{0}{1},", telemetryNames, subTelemetryName); } if (telemetryNames.Length > 0) { telemetryNames = telemetryNames.Substring(0, telemetryNames.Length - 1); } telemetryProperties.Add("Events", telemetryNames); try { IList <string> sourceEventMessages = new List <string>(); IList <Microsoft.ServiceBus.Messaging.EventData> brokeredMessages = new List <Microsoft.ServiceBus.Messaging.EventData>(sourceEvents.Count); foreach (TEvent @event in sourceEvents) { if (!AzureBusHelper.PrepareAndValidateEvent(@event, "Azure-EventHub")) { continue; } var brokeredMessage = new Microsoft.ServiceBus.Messaging.EventData(Encoding.UTF8.GetBytes(MessageSerialiser.SerialiseEvent(@event))); brokeredMessage.Properties.Add("Type", @event.GetType().FullName); brokeredMessages.Add(brokeredMessage); sourceEventMessages.Add(string.Format("A command was sent of type {0}.", @event.GetType().FullName)); } try { EventHubPublisher.SendBatch(brokeredMessages); wasSuccessfull = true; } catch (QuotaExceededException exception) { responseCode = "429"; Logger.LogError("The size of the event being sent was too large.", exception: exception, metaData: new Dictionary <string, object> { { "Events", sourceEvents } }); throw; } catch (Exception exception) { responseCode = "500"; Logger.LogError("An issue occurred while trying to publish a event.", exception: exception, metaData: new Dictionary <string, object> { { "Events", sourceEvents } }); throw; } foreach (string message in sourceEventMessages) { Logger.LogInfo(message); } wasSuccessfull = true; } finally { mainStopWatch.Stop(); TelemetryHelper.TrackDependency("Azure/EventHub/EventBus", "Event", telemetryName, null, startedAt, mainStopWatch.Elapsed, responseCode, wasSuccessfull, telemetryProperties); } }
/// <summary> /// Publishes the provided <paramref name="commands"/> on the command bus. /// </summary> public virtual void Publish <TCommand>(IEnumerable <TCommand> commands) where TCommand : ICommand <TAuthenticationToken> { if (commands == null) { Logger.LogDebug("No commands to publish."); return; } IList <TCommand> sourceCommands = commands.ToList(); if (!sourceCommands.Any()) { Logger.LogDebug("An empty collection of commands to publish."); return; } DateTimeOffset startedAt = DateTimeOffset.UtcNow; Stopwatch mainStopWatch = Stopwatch.StartNew(); string responseCode = "200"; bool wasSuccessfull = false; IDictionary <string, string> telemetryProperties = new Dictionary <string, string> { { "Type", "Azure/EventHub" } }; string telemetryName = "Commands"; string telemetryNames = string.Empty; foreach (TCommand command in sourceCommands) { string subTelemetryName = string.Format("{0}/{1}", command.GetType().FullName, command.Id); var telemeteredCommand = command as ITelemeteredMessage; if (telemeteredCommand != null) { subTelemetryName = telemeteredCommand.TelemetryName; } telemetryNames = string.Format("{0}{1},", telemetryNames, subTelemetryName); } if (telemetryNames.Length > 0) { telemetryNames = telemetryNames.Substring(0, telemetryNames.Length - 1); } telemetryProperties.Add("Commands", telemetryNames); try { IList <string> sourceCommandMessages = new List <string>(); IList <EventData> brokeredMessages = new List <EventData>(sourceCommands.Count); foreach (TCommand command in sourceCommands) { if (!AzureBusHelper.PrepareAndValidateCommand(command, "Azure-EventHub")) { continue; } var brokeredMessage = CreateBrokeredMessage(MessageSerialiser.SerialiseCommand, command.GetType(), command); brokeredMessages.Add(brokeredMessage); sourceCommandMessages.Add(string.Format("A command was sent of type {0}.", command.GetType().FullName)); } try { if (brokeredMessages.Any()) { #if NET452 EventHubPublisher.SendBatch(brokeredMessages); #endif #if NETSTANDARD2_0 EventHubPublisher.SendAsync(brokeredMessages).Wait(); #endif } else { Logger.LogDebug("An empty collection of commands to publish post validation."); } } catch (Exception exception) { responseCode = "500"; Logger.LogError("An issue occurred while trying to publish a command.", exception: exception, metaData: new Dictionary <string, object> { { "Commands", sourceCommands } }); throw; } foreach (string message in sourceCommandMessages) { Logger.LogInfo(message); } wasSuccessfull = true; } finally { mainStopWatch.Stop(); TelemetryHelper.TrackDependency("Azure/EventHub/CommandBus", "Command", telemetryName, null, startedAt, mainStopWatch.Elapsed, responseCode, wasSuccessfull, telemetryProperties); } }