Exemple #1
0
        public async Task EventHubConnectSend(int sendCount)
        {
            var cs = @"Endpoint=sb://yournamespace.servicebus.windows.net/;SharedAccessKeyName=YourPublisher;SharedAccessKey=12EHa5NNllnZBz6gksogvNfhketIokdTdjx4ZrTfu9U=";

            var builder = new ServiceBusConnectionStringBuilder(cs)
                        {
                            TransportType = TransportType.Amqp
                        };

            var client = EventHubClient.CreateFromConnectionString(builder.ToString(), "swiftsoftware-eh");

            int sentCount = sendCount;
            string infoText;

            for (int i = 0; i < sendCount; i++)
            {
                try
                {
                    if (MessageType.Text == "Sensor")
                    {
                        infoText = "Operating Within Expected Parameters";
                    }
                    else
                    {
                        infoText = "Temperature Sensor Requires Calibration";
                    }

                    Random rnd = new Random();
                    int intTemp = rnd.Next(15, 25);
                    int intHum = rnd.Next(50, 80);
                 
                    var e = new Event
                    {
                        MessageType = MessageType.Text,
                        Temp = intTemp,
                        Humidity = intHum,
                        Location = Location.Text,
                        Room = Room.Text,
                        Info = infoText
                    };

                    var serializedString = JsonConvert.SerializeObject(e);
                    var data = new EventData(Encoding.UTF8.GetBytes(serializedString))
                    {
                        PartitionKey = rnd.Next(6).ToString()
                    };

                    // Set user properties if needed
                    data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString());
                    
                    await client.SendAsync(data).ConfigureAwait(false);
                }
                catch (Exception exp)
                {
                    Console.WriteLine("Error on send: " + exp.Message);
                    sentCount--;
                }

                await Task.Delay(Convert.ToInt32(DelayTime.Text));
               
            }

            MessageResult.Text = " " + sentCount.ToString() + " " + MessageType.Text + " Messages were successfully sent to Event Hub";
            return;
        }
Exemple #2
0
        public void EventHubConnectSend(int sendCount)
        {
            string serviceBusConnectionString = ConfigurationManager.AppSettings["Microsoft.ServiceBus.ConnectionString"];

            if (string.IsNullOrWhiteSpace(serviceBusConnectionString))
            {
                Console.WriteLine("Please provide ServiceBus Connection string in App.Config.");
            }

            ServiceBusConnectionStringBuilder connectionString = new ServiceBusConnectionStringBuilder(serviceBusConnectionString);

            string ServiceBusNamespace = connectionString.Endpoints.First().Host;
            string namespaceKeyName = connectionString.SharedAccessKeyName;
            string namespaceKey = connectionString.SharedAccessKey;
            string baseAddressHttp = "http://" + ServiceBusNamespace + "/";
            string eventHubAddress = baseAddressHttp + EventHubName;

            var sas = CreateSasToken(ServiceBusNamespace, namespaceKeyName, namespaceKey);

            // Create client.
            var httpClient = new HttpClient();
            httpClient.BaseAddress = new Uri(String.Format("https://{0}/", ServiceBusNamespace));
            httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Authorization", sas);

            int sentCount = sendCount;
            string infoText;

            // Keep sending.
            for (int i = 0; i < sendCount; i++)
            {

                if (MessageType.Text == "Sensor")
                {
                    infoText = "Operating Within Expected Parameters";
                }
                else
                {
                    infoText = "Temperature Sensor Requires Calibration";
                }

                Random rnd = new Random();
                int intTemp = rnd.Next(15, 25);
                int intHum = rnd.Next(50, 80);

                Event e = new Event
                {
                    MessageType = MessageType.Text,
                    Temp = intTemp,
                    Humidity = intHum,
                    Location = Location.Text,
                    Room = Room.Text,
                    Info = infoText
                };


                //var serializedString = JsonConvert.SerializeObject(e);
                //var data = new EventData(Encoding.UTF8.GetBytes(serializedString))
                //{
                //    PartitionKey = rnd.Next(6).ToString()
                //};

                //// Set user properties if needed
                //data.Properties.Add("Type", "Telemetry_" + DateTime.Now.ToLongTimeString());

                var postResult = httpClient.PostAsJsonAsync(String.Format("{0}/messages", EventHubName), e).Result;

                if (postResult.StatusCode.ToString() != "Created")
                {
                    sentCount--;
                }

                Thread.Sleep(new Random().Next(1000, 5000));
            }

            MessageResult.Text = " " + sentCount.ToString() + " " + MessageType.Text + " Messages were successfully sent to Event Hub";
            return;
        }