示例#1
0
        // Sends {n} messages to event hub.
        private static async Task SendMessagesToEventHub(int numMessagesToSend)
        {
            for (var i = 0; i < numMessagesToSend; i++)
            {
                try
                {
                    var tEvent = new TempratureInfo()
                    {
                        Id = Guid.NewGuid(),
                        TempratureCelcius = GetRandomNumber(50, 500),
                        SensorId          = "SEN-001E"
                    };

                    string message = JsonConvert.SerializeObject(tEvent);

                    messages.Add(tEvent);

                    Console.WriteLine($"Sending message: {message}");

                    await eventHubClient.SendAsync(new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
                }

                await Task.Delay(500);
            }

            Console.WriteLine($"{numMessagesToSend} messages sent.");
        }
示例#2
0
        // Sends {n} messages to event hub.
        private static async Task SendMessagesToEventHub(int numMessagesToSend)
        {
            int j = 0;

            for (var i = 0; i < numMessagesToSend; i++)
            {
                try
                {
                    var tEvent = new TempratureInfo();

                    tEvent = new TempratureInfo()
                    {
                        Id = i.ToString(),
                        TempratureCelcius = i % 100 == 0 ? 600 : GetRandomNumber(350, 375),
                        SensorId          = "H-Sensor-01",
                        EventTime         = DateTime.Now
                    };

                    if (i > 200 && i < 220)
                    {
                        j++;

                        tEvent = new TempratureInfo()
                        {
                            Id = i.ToString(),
                            TempratureCelcius = 375 + j * 5,
                            SensorId          = "H-Sensor-01",
                            EventTime         = DateTime.Now
                        };
                    }

                    string message = JsonConvert.SerializeObject(tEvent);

                    messages.Add(tEvent);

                    Console.WriteLine($"Sending message: {message}");

                    await eventHubClient.SendAsync(
                        new EventData(Encoding.UTF8.GetBytes(message)));
                }
                catch (Exception exception)
                {
                    Console.WriteLine($"{DateTime.Now} > Exception: {exception.Message}");
                }

                await Task.Delay(250);
            }

            Console.WriteLine($"{numMessagesToSend} messages sent.");
        }