private async Task HandleMessageWithContent(IMessage <T> message) { try { Log.Debug($"Processing message of type {typeof(T).FullName}", message.Content.ToDictionary()); try { _messageContextProvider.StoreMessageContext(message); await ProcessMessage(message.Content); } finally { _messageContextProvider.ReleaseMessageContext(message); } await message.CompleteAsync(); Log.Info($"Completed message {typeof(T).FullName}"); } catch (Exception ex) { Log.Error(ex, $"Failed to process message {typeof(T).FullName}"); if (message?.Content != null) { await message.AbortAsync(); } await OnErrorAsync(message, ex); } }
public async Task Run(CancellationTokenSource cancellationTokenSource) { using (var subscriber = _messageSubscriberFactory.GetSubscriber <T>()) { while (!cancellationTokenSource.Token.IsCancellationRequested) { IMessage <T> message; Log.Debug($"Getting message of type {typeof(T).FullName} from azure topic message queue"); try { message = await subscriber.ReceiveAsAsync(); } catch (Exception ex) { Log.Fatal(ex, $"Failed to retrieve message {typeof(T).FullName}"); await OnFatalAsync(ex); cancellationTokenSource.Cancel(); throw; } Log.Debug($"Received message of type {typeof(T).FullName} from azure topic message queue"); try { if (message == null) { Log.Debug($"No messages on queue of type {typeof(T).FullName}"); await Task.Delay(500, cancellationTokenSource.Token); continue; } if (message.Content == null) { Log.Debug($"Message of type {typeof(T).FullName} has null content"); await message.CompleteAsync(); continue; } Log.Debug($"Processing message of type {typeof(T).FullName}", message.Content.ToDictionary()); try { _messageContextProvider.StoreMessageContext(message); await ProcessMessage(message.Content); } finally { _messageContextProvider.ReleaseMessageContext(message); } await message.CompleteAsync(); Log.Info($"Completed message {typeof(T).FullName}"); } catch (Exception ex) { Log.Error(ex, $"Failed to process message {typeof(T).FullName}"); if (message != null && message.Content != null) { await message.AbortAsync(); } await OnErrorAsync(message, ex); } } } }