I am currently doing something like this to get all available telemetry (device to cloud messages).ReadEventsAsync
would block once there are no new messages. I know I can specify a time period but then that will be a wait game. Is there a way for me to determine how many messages are currently available ?
I wanted to know if there is a way for me to stop listening for messages once I have consumed all previous messages.
The only way I can think of right now is to know the no of available messages
Task.Run(async () =>
{
var cancellationSource = new CancellationTokenSource();
await using EventHubConsumerClient consumer = new EventHubConsumerClient(EventHubConsumerClient.DefaultConsumerGroupName, connectionString, DeviceHubConstants.EventHubName);
try
{
await foreach (PartitionEvent partitionEvent in consumer.ReadEventsAsync( cancellationSource.Token))
{
string data = Encoding.UTF8.GetString(partitionEvent.Data.Body.ToArray());
}
}
catch (TaskCanceledException)
{
}
}).GetAwaiter().GetResult();