// Asynchronously create a PartitionReceiver for a partition and then start // reading any messages sent from the simulated client. private async Task ReceiveMessagesFromDeviceAsync(string partition, CancellationToken ct, SensorsData sensorsData) { // Create the receiver using the default consumer group. // For the purposes of this sample, read only messages sent since // the time the receiver is created. Typically, you don't want to skip any messages. var eventHubReceiver = s_eventHubClient.CreateReceiver("$Default", partition, EventPosition.FromEnqueuedTime(DateTime.Now)); Console.WriteLine("Create receiver on partition: " + partition); while (true) { //if (ct.IsCancellationRequested) break; Console.WriteLine("Listening for messages on: " + partition); // Check for EventData - this methods times out if there is nothing to retrieve. var events = await eventHubReceiver.ReceiveAsync(100); // If there is data in the batch, process it. if (events == null) { continue; } foreach (EventData eventData in events) { string data = Encoding.UTF8.GetString(eventData.Body.Array); Console.WriteLine("Message received on partition {0}:", partition); Console.WriteLine(" {0}:", data); Console.WriteLine("Application properties (set by device):"); foreach (var prop in eventData.Properties) { Console.WriteLine(" {0}: {1}", prop.Key, prop.Value); } Console.WriteLine("System properties (set by IoT Hub):"); foreach (var prop in eventData.SystemProperties) { Console.WriteLine(" {0}: {1}", prop.Key, prop.Value); } sensorsData.AddData(eventData); } } }