/// <summary> /// Processes a request to synchronise TraceIdentifier and Correlation ID headers. Also creates a /// <see cref="CommandContext" /> for the current request and disposes of it when the request is completing. /// </summary> /// <param name="context">The <see cref="HttpContext" /> for the current request.</param> /// <param name="commandContextAccessor"> /// The <see cref="IStoveCommandContextAccessor" /> which can create a /// <see cref="CommandContext" />. /// </param> /// <returns></returns> public async Task Invoke(HttpContext context, IStoveCommandContextAccessor commandContextAccessor) { if (context.Request.Headers.TryGetValue(_options.Value.Header, out StringValues correlationId)) { context.TraceIdentifier = correlationId; } else { correlationId = Guid.NewGuid().ToString(); context.TraceIdentifier = correlationId; } using (commandContextAccessor.Use(correlationId)) { if (_options.Value.IncludeInResponse) { // apply the correlation ID to the response header for client side tracking context.Response.OnStarting(() => { if (!context.Response.Headers.ContainsKey(_options.Value.Header)) { context.Response.Headers.Add(_options.Value.Header, context.TraceIdentifier); } return(Task.CompletedTask); }); } await _next(context); } }
public static string GetCorrelationIdOrEmpty(this IStoveCommandContextAccessor accessor) { if (accessor.CommandContext != null) { return(accessor.CommandContext.CorrelationId); } return(Guid.Empty.ToString()); }
public ValuesController(IStoveCommandContextAccessor commandContextAccessor) { _commandContextAccessor = commandContextAccessor; }