/// <summary> /// Invokes the middleware for the current <paramref name="httpContext"/>. /// </summary> /// <param name="httpContext">The current <see cref="HttpContext"/>.</param> /// <returns>An awaitable to wait for to complete the request.</returns> public Task Invoke(HttpContext httpContext) { if (httpContext is null) { throw new ArgumentNullException(nameof(httpContext)); } // ReSharper disable once UseDeconstruction - not supported in .NET46/.NETS KeyValuePair <string, string?> requestHeader = httpContext.Request.Headers.GetCorrelationIdHeader(_acceptedRequestHeaders); if (requestHeader.Value != null) { _logger.LogTrace("Request header '{HeaderName}' found with correlation id '{CorrelationId}'.", requestHeader.Key, requestHeader.Value); } string?responseHeaderName = _options.IncludeInResponse ? requestHeader.Key : null; var correlatedHttpRequest = new HttpRequestActivity(_logger, httpContext, responseHeaderName); return(_asyncCorrelationManager.CorrelateAsync( requestHeader.Value, () => { correlatedHttpRequest.Start(_correlationContextAccessor.CorrelationContext); return _next(httpContext) .ContinueWith(t => { correlatedHttpRequest.Stop(); return t; }, TaskScheduler.Current) .Unwrap(); })); }
/// <summary> /// Invokes the middleware for the current <paramref name="httpContext"/>. /// </summary> /// <param name="httpContext">The current <see cref="HttpContext"/>.</param> /// <returns>An awaitable to wait for to complete the request.</returns> public Task Invoke(HttpContext httpContext) { KeyValuePair <string, string> requestHeader = httpContext.Request.Headers.GetCorrelationIdHeader(_acceptedRequestHeaders); if (requestHeader.Value != null) { _logger.LogTrace("Request header '{HeaderName}' found with correlation id '{CorrelationId}'.", requestHeader.Key, requestHeader.Value); } string responseHeaderName = _options.IncludeInResponse ? requestHeader.Key : null; var correlatedHttpRequest = new HttpRequestActivity(_logger, httpContext, responseHeaderName); return(_asyncCorrelationManager.CorrelateAsync( requestHeader.Value, () => { correlatedHttpRequest.Start(_correlationContextAccessor.CorrelationContext); return _next(httpContext) .ContinueWith(t => { correlatedHttpRequest.Stop(); return t; }) .Unwrap(); })); }
public void Dispose() { _sut.Stop(); }