public async Task InvokeHandlers <TPayload>(string json, IJobCancellationToken jobCancellationToken) where TPayload : ActivityPayload { logger.LogTrace("Beginning payload processing job: {0}"); var cancellationToken = jobCancellationToken.ShutdownToken; var payload = new SimpleJsonSerializer().Deserialize <TPayload>(json); var tasks = new List <Task>(); async Task RunHandler(IPayloadHandler <TPayload> payloadHandler) { try { await payloadHandler.ProcessPayload(payload, cancellationToken).ConfigureAwait(false); } //To be expected catch (OperationCanceledException e) { logger.LogDebug(e, "Payload handler processing cancelled!"); } catch (NotSupportedException e) { logger.LogTrace(e, "Payload handler does not support payload!"); } catch (Exception e) { logger.LogError(e, "Payload handler threw exception!"); } }; await componentProvider.Initialize(cancellationToken).ConfigureAwait(false); foreach (var handler in componentProvider.GetPayloadHandlers <TPayload>()) { tasks.Add(RunHandler(handler)); } await Task.WhenAll(tasks).ConfigureAwait(false); if (autoMergeHandler is IPayloadHandler <TPayload> asHandler) { logger.LogTrace("Running auto merge payload handler."); try { await asHandler.ProcessPayload(payload, cancellationToken).ConfigureAwait(false); } catch (Exception e) { logger.LogError(e, "Failed running auto merge handler!"); } } else { logger.LogTrace("Not running auto merge handler to to payload type of {0}.", typeof(TPayload).FullName); } }