コード例 #1
0
        /// <summary>
        ///     <see cref="SendApplicationMetadata" /> sends
        ///     <see cref="applicationMetadataEvent" /> to the Event Hub instance
        ///     associated with <see cref="eventHubClient" />.
        /// </summary>
        /// <param name="eventHubClient">
        ///     <see cref="eventHubClient" /> is the Event Hub client proxy.
        /// </param>
        /// <param name="applicationMetadataEvent">
        ///     <see cref="ApplicationMetadataEvent" /> is an event that contains metadata
        ///     originating from an imaginary up-stream web application.
        /// </param>
        /// <param name="partitionKey">
        ///     <see cref="partitionKey" /> is the Event Hub Partition Key that determines
        ///     the Event Hub Partition to which
        ///     <see cref="applicationMetadataEvent" /> will be sent.
        /// </param>
        private static void SendApplicationMetadata(EventHubClient eventHubClient,
                                                    ApplicationMetadataEvent applicationMetadataEvent, string partitionKey)
        {
            var serialisedApplicationMetadata = JsonConvert.SerializeObject(applicationMetadataEvent);

            var eventData = new EventData(Encoding.UTF8.GetBytes(serialisedApplicationMetadata))
            {
                PartitionKey = partitionKey
            };

            eventHubClient.Send(eventData);
        }
コード例 #2
0
        /// <summary>
        ///     <see cref="SendRandomApplicationMetadataEvents" /> sends randomly-generated
        ///     <see cref="ApplicationMetadataEvent" /> instances to Event Hub.
        /// </summary>
        private static void SendRandomApplicationMetadataEvents()
        {
            EventHubClient eventHubClient;

            try
            {
                var eventHubName             = ConfigurationManager.AppSettings["EventHubName"];
                var eventHubConnectionString = ConfigurationManager.AppSettings["EventHubConnectionString"];

                eventHubClient = EventHubClient
                                 .CreateFromConnectionString(eventHubConnectionString, eventHubName);
            }
            catch (Exception exception)
            {
                Console.WriteLine("A problem occurred connecting to Event Hub: " + exception);
                return;
            }

            var random = new Random();
            var values = Enum.GetValues(typeof(Device));

            const int eventCount = 250;

            for (var j = 0; j < eventCount; j++)
            {
                foreach (var partitionKey in _partitionKeys)
                {
                    try
                    {
                        var applicationMetadata = new ApplicationMetadataEvent
                        {
                            IPAddress = GenerateRandomIPAddress(random),
                            Time      = DateTime.UtcNow,
                            Device    = (Device)values.GetValue(random.Next(1, values.Length))
                        };

                        SendApplicationMetadata(eventHubClient, applicationMetadata, partitionKey);
                    }
                    catch (Exception exception)
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("{0} > Exception: {1}", DateTime.Now, exception.Message);
                        Console.ResetColor();
                    }
                }
            }

            Console.ForegroundColor = ConsoleColor.Green;
            Console.WriteLine("Sent {0} events.", eventCount * _partitionKeys.Count);
            Console.ResetColor();
            Console.ReadLine();
        }