Skip to content
This repository was archived by the owner on Nov 16, 2023. It is now read-only.

Commit ed5a3c4

Browse files
author
David R. Williamson
authored
Merge pull request #137 from Azure-Samples/drwill/messageRetryDispose
Ensure message is retried and disposed
2 parents 6097ef1 + 189db84 commit ed5a3c4

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

iot-hub/Samples/device/DeviceReconnectionSample/DeviceReconnectionSample.cs

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,28 @@ private async Task SendMessagesAsync(CancellationToken cancellationToken)
194194
{
195195
_logger.LogInformation($"Device sending message {++messageCount} to IoT Hub...");
196196

197-
try
197+
Message message = PrepareMessage(messageCount);
198+
while (true)
198199
{
199-
await SendMessageAsync(messageCount);
200-
}
201-
catch (IotHubException ex) when (ex.IsTransient)
202-
{
203-
// Inspect the exception to figure out if operation should be retried, or if user-input is required.
204-
_logger.LogError($"An IotHubException was caught, but will try to recover and retry explicitly: {ex}");
205-
}
206-
catch (Exception ex) when (ExceptionHelper.IsNetworkExceptionChain(ex))
207-
{
208-
_logger.LogError($"A network related exception was caught, but will try to recover and retry explicitly: {ex}");
200+
try
201+
{
202+
await s_deviceClient.SendEventAsync(message);
203+
_logger.LogInformation($"Sent message {messageCount} with data [{message.GetBytes()}]");
204+
message.Dispose();
205+
continue;
206+
}
207+
catch (IotHubException ex) when (ex.IsTransient)
208+
{
209+
// Inspect the exception to figure out if operation should be retried, or if user-input is required.
210+
_logger.LogError($"An IotHubException was caught, but will try to recover and retry: {ex}");
211+
}
212+
catch (Exception ex) when (ExceptionHelper.IsNetworkExceptionChain(ex))
213+
{
214+
_logger.LogError($"A network related exception was caught, but will try to recover and retry: {ex}");
215+
}
216+
217+
// wait and retry
218+
await Task.Delay(s_sleepDuration);
209219
}
210220
}
211221

@@ -246,22 +256,21 @@ private async Task ReceiveMessagesAsync(CancellationToken cancellationToken)
246256
}
247257
}
248258

249-
private async Task SendMessageAsync(int messageId)
259+
private Message PrepareMessage(int messageId)
250260
{
251261
var temperature = s_randomGenerator.Next(20, 35);
252262
var humidity = s_randomGenerator.Next(60, 80);
253263
string messagePayload = $"{{\"temperature\":{temperature},\"humidity\":{humidity}}}";
254264

255-
using var eventMessage = new Message(Encoding.UTF8.GetBytes(messagePayload))
265+
var eventMessage = new Message(Encoding.UTF8.GetBytes(messagePayload))
256266
{
257267
MessageId = messageId.ToString(),
258268
ContentEncoding = Encoding.UTF8.ToString(),
259269
ContentType = "application/json",
260270
};
261271
eventMessage.Properties.Add("temperatureAlert", (temperature > TemperatureThreshold) ? "true" : "false");
262272

263-
await s_deviceClient.SendEventAsync(eventMessage);
264-
_logger.LogInformation($"Sent message {messageId} with data [{messagePayload}]");
273+
return eventMessage;
265274
}
266275

267276
private async Task ReceiveMessageAndCompleteAsync()

0 commit comments

Comments
 (0)