/// <summary> /// Perform an asynchronous operation with the CloudEvent payload /// </summary> /// <param name="context"></param> /// <param name="asyncAction"></param> /// <returns></returns> protected async Task ConsumeCloudEvent(ConsumeContext <T> context, Func <object, Task> asyncAction) { try { _logger.LogInformation($"Consume: received '{typeof(T)}' with MessageId = '{context.MessageId}'"); object payload = context.GetCloudEventsPayload(_ceReader); await asyncAction(payload); switch (payload) { case ConsumerPayload consumerPayload: _logger.LogInformation( $"Consume: Payload for MessageId '{context.MessageId}' has mapped type = '{consumerPayload.GetType().Name}', content = '{JsonConvert.SerializeObject(consumerPayload)}'"); break; default: _logger.LogWarning($"'{this.GetType().Name}' cannot consume payloads of type {payload.GetType().Name}"); break; } } catch (Exception ex) { var errDetails = ex.InnerException != null? ex.InnerException.GetType().Name + " - " + ex.InnerException.Message: ex.StackTrace ?? "NA"; _logger.LogError($"Consume: failed with exception '{ex.Message}', of type '{ex.GetType().Name}', details '{errDetails}'"); } }
/// <summary> /// Perform a synchronous operation with the CloudEvent payload /// </summary> /// <param name="context"></param> /// <param name="action"></param> /// <returns></returns> protected async Task ConsumeCloudEvent(ConsumeContext <T> context, Action <object> action) { try { _logger.LogInformation($"Consume: received '{typeof(T)}' with MessageId = '{context.MessageId}'"); object payload = context.GetCloudEventsPayload(_ceReader); action(payload); } catch (Exception ex) { var errDetails = ex.InnerException != null? ex.InnerException.GetType().Name + " - " + ex.InnerException.Message: ex.StackTrace ?? "NA"; _logger.LogError($"Consume: failed with exception '{ex.Message}', of type '{ex.GetType().Name}', details '{errDetails}'"); } await Task.CompletedTask; }