protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { if (request.Headers.All(x => !string.Equals(x.Key, CorrelationHeader.Key, StringComparison.InvariantCultureIgnoreCase))) { request.Headers.TryAddWithoutValidation(CorrelationHeader.Key, _correlationService.GetContext().DgpHeader); } return(base.SendAsync(request, cancellationToken)); }
private Guid GetIdentifier() { if (!Guid.TryParse(_correlationService.GetContext().Id, out var identifier)) { identifier = Guid.NewGuid(); } return(identifier); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var correlationHeader = request?.Headers?.FirstOrDefault(x => x.Key.ToLowerInvariant() == CorrelationHeader.Key.ToLowerInvariant()); if (!String.IsNullOrWhiteSpace(correlationHeader?.Key)) { request.Headers.Remove(correlationHeader.Value.Key); } request.Headers.Add(CorrelationHeader.Key, _correlationService.GetContext().DgpHeader); return(await base.SendAsync(request, cancellationToken)); }
public Task Invoke(HttpContext context) { var correlationHeader = context.Request?.Headers?.FirstOrDefault(x => x.Key.ToLowerInvariant() == CorrelationHeader.Key.ToLowerInvariant()).Value; var correlationHeaderValue = !correlationHeader.HasValue || !correlationHeader.Value.Any() ? String.Empty : correlationHeader.Value.FirstOrDefault(); if (_options.Value.CorrelationHeaderRequired && !Regex.IsMatch(context.Request.Path, _options.Value.CorrelationHeaderNotRequiredRouteRegex)) { if (StringValues.IsNullOrEmpty(correlationHeaderValue)) { _logger.LogWarning("CorrelationHeader is required."); var exception = new ValidationException("CorrelationHeader is required.", ErrorCode.RequiredCorrelationHeader); exception.AddMessage("CorrelationHeader", "CorrelationHeader is required."); throw exception; } } if (string.IsNullOrWhiteSpace(correlationHeaderValue)) { correlationHeaderValue = _correlationService.GetContext().DgpHeader; } _correlationContextFormatter.ValidateAndSetPropertiesFromDgpHeader(correlationHeaderValue); return(_next.Invoke(context)); }
protected override async Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { request.Headers.TryAddWithoutValidation(CorrelationHeader.Key, _correlationService.GetContext().DgpHeader); return(await base.SendAsync(request, cancellationToken)); }