예제 #1
0
        static void Main(string[] args)
        {
            var settings = new MessagingFactorySettings()
            {
                TokenProvider = TokenProvider.CreateSharedAccessSignatureTokenProvider(sharedAccessPolicyName, sharedAccessPolicyKey),
                TransportType = TransportType.Amqp
            };
            var factory = MessagingFactory.Create(
                 ServiceBusEnvironment.CreateServiceUri("sb", eventHubNamespace, ""), settings);

            EventHubClient client = factory.CreateEventHubClient(eventHubName);

            try
            {

                List<Task> tasks = new List<Task>();
                // Send messages to Event Hub
                Console.WriteLine("Sending messages to Event Hub {0}", client.Path);
                Random random = new Random();
                //for (int i = 0; i < numberOfMessages; ++i)
                while(!Console.KeyAvailable)
                {
                    // One event per device
                    for(int devices = 0; devices < numberOfDevices; devices++)
                    {
                        // Create the device/temperature metric
                        Event info = new Event() {
                            TimeStamp = DateTime.UtcNow,
                            DeviceId = random.Next(numberOfDevices),
                            Temperature = random.Next(100)
                        };
                        // Serialize to JSON
                        var serializedString = JsonConvert.SerializeObject(info);
                        Console.WriteLine(serializedString);
                        EventData data = new EventData(Encoding.UTF8.GetBytes(serializedString))
                        {
                            PartitionKey = info.DeviceId.ToString()
                        };

                        // Send the metric to Event Hub
                        tasks.Add(client.SendAsync(data));
                    }
                    // Sleep a second
                    Thread.Sleep(1000);
                };

                Task.WaitAll(tasks.ToArray());
            }
            catch (Exception exp)
            {
                Console.WriteLine("Error on send: " + exp.Message);
            }
        }
예제 #2
0
        static void Main(string[] args)
        {
            // Get values from configuration
            int numberOfDevices = int.Parse(ConfigurationManager.AppSettings["NumberOfDevices"]);
            string eventHubName = ConfigurationManager.AppSettings["EventHubName"];
            string eventHubNamespace = ConfigurationManager.AppSettings["EventHubNamespace"];
            string devicesSharedAccessPolicyName = ConfigurationManager.AppSettings["DevicesSharedAccessPolicyName"];
            string devicesSharedAccessPolicyKey = ConfigurationManager.AppSettings["DevicesSharedAccessPolicyKey"];

            string eventHubConnectionString = string.Format("Endpoint=sb://{0}.servicebus.windows.net/;SharedAccessKeyName={1};SharedAccessKey={2};TransportType=Amqp",
                eventHubNamespace, devicesSharedAccessPolicyName, devicesSharedAccessPolicyKey);

            var client = EventHubClient.CreateFromConnectionString(eventHubConnectionString, eventHubName);

            // Configure JSON to serialize properties using camelCase
            JsonConvert.DefaultSettings = () => new JsonSerializerSettings {ContractResolver = new CamelCasePropertyNamesContractResolver()};

            var random = new Random();

            try
            {
                Console.WriteLine("Sending messages to Event Hub {0}", client.Path);

                while (!Console.KeyAvailable)
                {
                    var tasks = new List<Task>();

                    // One event per device
                    for (int devices = 0; devices < numberOfDevices; devices++)
                    {
                        // Create the event
                        var info = new Event()
                        {
                            Id = devices.ToString(),
                            Lat = -30 + random.Next(75),
                            Lng = -120 + random.Next(70),
                            Time = DateTime.UtcNow.Ticks,
                            Code = (310 + random.Next(20)).ToString()
                        };

                        // Serialize to JSON
                        var serializedString = JsonConvert.SerializeObject(info);
                        Console.WriteLine(serializedString);

                        // Create the message data
                        var bytes = Encoding.UTF8.GetBytes(serializedString);
                        var data = new EventData(bytes)
                        {
                            PartitionKey = info.Id
                        };

                        // Send the message to Event Hub
                        tasks.Add(client.SendAsync(data));
                    }

                    Task.WaitAll(tasks.ToArray());
                }
            }
            catch (Exception exp)
            {
                Console.WriteLine("Error on send: " + exp.Message);
            }
        }