public Task Invoke(HttpContext context, IFhirRequestContextAccessor fhirRequestContextAccessor, CorrelationIdProvider correlationIdProvider) { HttpRequest request = context.Request; string baseUriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase); string uriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); var fhirRequestContext = new FhirRequestContext( method: request.Method, uriString: uriInString, baseUriString: baseUriInString, requestType: ValueSets.AuditEventType.RestFulOperation, correlationId: correlationIdProvider.Invoke(), requestHeaders: context.Request.Headers, responseHeaders: context.Response.Headers); if (context.User != null) { fhirRequestContext.Principal = context.User; } fhirRequestContextAccessor.FhirRequestContext = fhirRequestContext; // Call the next delegate/middleware in the pipeline return(_next(context)); }
public async Task InvokeAsync(HttpContext context) { var header = context.Request.Headers[Constants.CorrelationIDHeader]; string correlationId; if (header.Count > 0) { correlationId = header[0]; } else { correlationId = _correlationIdProvider.Invoke(); } context.Items[Constants.CorrelationIDHeader] = correlationId; var logger = context.RequestServices.GetRequiredService <ILogger <CorrelationMiddleware> >(); //using (_loggerScope.BeginScope("{@CorrelationId}", correlationId)) using (logger.BeginScope(new Dictionary <string, object> { ["CorrelationId"] = correlationId })) { await this._next(context); } }
public async Task Invoke(HttpContext context, IFhirRequestContextAccessor fhirRequestContextAccessor, CorrelationIdProvider correlationIdProvider) { HttpRequest request = context.Request; string baseUriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase); string uriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); string correlationId = correlationIdProvider.Invoke(); var fhirRequestContext = new FhirRequestContext( method: request.Method, uriString: uriInString, baseUriString: baseUriInString, correlationId: correlationId, requestHeaders: context.Request.Headers, responseHeaders: context.Response.Headers); context.Response.Headers[RequestIdHeaderName] = correlationId; fhirRequestContextAccessor.FhirRequestContext = fhirRequestContext; // Call the next delegate/middleware in the pipeline await _next(context); }
public TargetClient(IHttpClientFactory httpClientFactory, CorrelationIdProvider correlationIdProvider, IOptions <BaseUrls> options) { _options = options.Value ?? throw new ArgumentNullException(nameof(options)); var correlationId = correlationIdProvider.Invoke(); _httpClient = httpClientFactory.CreateClient("MyClient"); _httpClient.BaseAddress = new System.Uri(_options.TargetClient); }
protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { var correlationId = _correlationIdProvider.Invoke(); request.Headers.Add(Constants.RequestIDHeader, correlationId); request.Headers.Add(Constants.CorrelationIDHeader, correlationId); return(base.SendAsync(request, cancellationToken)); }
public Task Invoke(HttpContext context, IFhirRequestContextAccessor fhirRequestContextAccessor, CorrelationIdProvider correlationIdProvider) { HttpRequest request = context.Request; string baseUriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase); string uriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); string correlationId = correlationIdProvider.Invoke(); object resourceType = null; RouteData routeData = context.GetRouteData(); if (routeData != null && routeData.Values != null) { routeData.Values.TryGetValue(KnownActionParameterNames.ResourceType, out resourceType); } var fhirRequestContext = new FhirRequestContext( method: request.Method, uriString: uriInString, baseUriString: baseUriInString, correlationId: correlationId, requestHeaders: context.Request.Headers, responseHeaders: context.Response.Headers, resourceType: resourceType?.ToString()); context.Response.Headers[RequestIdHeaderName] = correlationId; // Note that if this is executed before authentication occurs, the user will not contain any claims. if (context.User != null) { fhirRequestContext.Principal = context.User; } fhirRequestContextAccessor.FhirRequestContext = fhirRequestContext; // Call the next delegate/middleware in the pipeline return(_next(context)); }
public Task Invoke(HttpContext context, IFhirRequestContextAccessor fhirRequestContextAccessor, CorrelationIdProvider correlationIdProvider) { HttpRequest request = context.Request; string baseUriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase); string uriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); string correlationId = correlationIdProvider.Invoke(); var fhirRequestContext = new FhirRequestContext( method: request.Method, uriString: uriInString, baseUriString: baseUriInString, requestType: new CodingInfo(AuditEventType.System, AuditEventType.RestFulOperationCode), correlationId: correlationId, requestHeaders: context.Request.Headers, responseHeaders: context.Response.Headers); context.Response.Headers[RequestIdHeaderName] = correlationId; // Note that if this is executed before authentication occurs, the user will not contain any claims. if (context.User != null) { fhirRequestContext.Principal = context.User; } fhirRequestContextAccessor.FhirRequestContext = fhirRequestContext; // Call the next delegate/middleware in the pipeline return(_next(context)); }
public async Task Invoke(HttpContext context, RequestContextAccessor <IFhirRequestContext> fhirRequestContextAccessor, CorrelationIdProvider correlationIdProvider) { HttpRequest request = context.Request; string baseUriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase); string uriInString = UriHelper.BuildAbsolute( request.Scheme, request.Host, request.PathBase, request.Path, request.QueryString); string correlationId = correlationIdProvider.Invoke(); // https://www.hl7.org/fhir/http.html#custom // If X-Request-Id header is present, then put it value into X-Correlation-Id header for response. if (context.Request.Headers.TryGetValue(KnownHeaders.RequestId, out var requestId) && !string.IsNullOrEmpty(requestId)) { context.Response.Headers[KnownHeaders.CorrelationId] = requestId; } var fhirRequestContext = new FhirRequestContext( method: request.Method, uriString: uriInString, baseUriString: baseUriInString, correlationId: correlationId, requestHeaders: context.Request.Headers, responseHeaders: context.Response.Headers); context.Response.Headers[KnownHeaders.RequestId] = correlationId; fhirRequestContextAccessor.RequestContext = fhirRequestContext; // Call the next delegate/middleware in the pipeline await _next(context); }
public async Task Invoke(HttpContext context) { try { await _next(context); } catch (Exception exception) { if (context.Response.HasStarted) { _logger.LogWarning("The response has already started, the base exception middleware will not be executed."); throw; } var localCorrelationId = _fhirRequestContextAccessor.FhirRequestContext?.CorrelationId; if (string.IsNullOrWhiteSpace(localCorrelationId)) { localCorrelationId = _correlationIdProvider.Invoke(); _logger.LogError($"No correlation id available in exception middleware. Setting to {localCorrelationId}"); } context.Response.Clear(); var diagnostics = Resources.GeneralInternalError; // If any of these exceptions are encountered, show a more specific diagnostic message if (exception.Message.StartsWith("IDX10803: Unable to obtain configuration from:", StringComparison.OrdinalIgnoreCase)) { diagnostics = Resources.UnableToObtainOpenIdConfiguration; } else if (exception.Message.StartsWith("The MetadataAddress or Authority must use HTTPS", StringComparison.OrdinalIgnoreCase)) { diagnostics = Resources.RequireHttpsMetadataError; } var operationOutcome = new OperationOutcome { Id = localCorrelationId, Issue = new List <OperationOutcome.IssueComponent> { new OperationOutcome.IssueComponent { Severity = OperationOutcome.IssueSeverity.Fatal, Code = OperationOutcome.IssueType.Exception, Diagnostics = diagnostics, }, }, }; try { await _contentTypeService.CheckRequestedContentTypeAsync(context); } catch (UnsupportedMediaTypeException) { context.Response.ContentType = ContentType.JSON_CONTENT_HEADER; } var result = FhirResult.Create(operationOutcome, HttpStatusCode.InternalServerError); await ExecuteResultAsync(context, result); } }