Ejemplo n.º 1
0
        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 static MessagingEventData ToMessagingEventData(this EventData eventData)
        {
            string             eventDataSerialized = JsonConvert.SerializeObject(eventData);
            MessagingEventData messagingEventData  = new MessagingEventData(Encoding.UTF8.GetBytes(eventDataSerialized));

            return(messagingEventData);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Publishes the provided <paramref name="command"/> on the command bus.
        /// </summary>
        public virtual void Publish <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            if (command == null)
            {
                Logger.LogDebug("No command 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      = string.Format("{0}/{1}", command.GetType().FullName, command.Id);
            var    telemeteredCommand = command as ITelemeteredMessage;

            if (telemeteredCommand != null)
            {
                telemetryName = telemeteredCommand.TelemetryName;
            }
            telemetryName = string.Format("Command/{0}", telemetryName);

            try
            {
                if (!AzureBusHelper.PrepareAndValidateCommand(command, "Azure-EventHub"))
                {
                    return;
                }

                var brokeredMessage = new EventData(Encoding.UTF8.GetBytes(MessageSerialiser.SerialiseCommand(command)));
                brokeredMessage.Properties.Add("Type", command.GetType().FullName);

                try
                {
                    EventHubPublisher.Send(brokeredMessage);
                }
                catch (Exception exception)
                {
                    responseCode = "500";
                    Logger.LogError("An issue occurred while trying to publish a command.", exception: exception, metaData: new Dictionary <string, object> {
                        { "Command", command }
                    });
                    throw;
                }
                Logger.LogInfo(string.Format("A command was sent of type {0}.", command.GetType().FullName));
                wasSuccessfull = true;
            }
            finally
            {
                mainStopWatch.Stop();
                TelemetryHelper.TrackDependency("Azure/EventHub/CommandBus", "Command", telemetryName, null, startedAt, mainStopWatch.Elapsed, responseCode, wasSuccessfull, telemetryProperties);
            }
        }
Ejemplo n.º 4
0
        private static async void Service_OnDataReceived(Microsoft.ServiceBus.Messaging.EventData data)
        {
            var message = new HubMessage
            {
                //CreatedAt = DateTime.Now,
                Entity       = Encoding.UTF8.GetString(data.GetBytes()),
                PartitionKey = "metrics",
                RowKey       = Guid.NewGuid().ToString()
            };
            var status = await _dataTable.SaveDataToTable(message);

            Console.WriteLine("Message received {0}", status.HttpStatusCode);
        }
Ejemplo n.º 5
0
        public virtual void Publish <TCommand>(TCommand command)
            where TCommand : ICommand <TAuthenticationToken>
        {
            if (!AzureBusHelper.PrepareAndValidateCommand(command, "Azure-EventHub"))
            {
                return;
            }

            var brokeredMessage = new EventData(Encoding.UTF8.GetBytes(MessageSerialiser.SerialiseCommand(command)));

            brokeredMessage.Properties.Add("Type", command.GetType().FullName);

            EventHubPublisher.Send(brokeredMessage);
            Logger.LogInfo(string.Format("A command was sent of type {0}.", command.GetType().FullName));
        }
 public static MessagingEventData ToMessagingEventData(this EventData eventData)
 {
     string eventDataSerialized = JsonConvert.SerializeObject(eventData);
     MessagingEventData messagingEventData = new MessagingEventData(Encoding.UTF8.GetBytes(eventDataSerialized));
     return messagingEventData;
 }
Ejemplo n.º 7
0
        /// <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 = 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));
                }

                try
                {
                    if (brokeredMessages.Any())
                    {
                        EventHubPublisher.SendBatch(brokeredMessages);
                    }
                    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);
            }
        }