internal void FillCorrelationContext(string correlationHeader, CorrelationContext context)
        {
            try
            {
                byte[] data = Convert.FromBase64String(correlationHeader);
                string json = Encoding.UTF8.GetString(data);

                dynamic parsedHeader  = JObject.Parse(json);
                string  correlationId = (string)parsedHeader.id;

                if (string.IsNullOrEmpty(correlationId))
                {
                    _logger.LogWarning("Invalid correlationheader, id is required." + correlationHeader);
                    var exception = new ValidationException("Invalid correlationHeader, id is required.", "REQCOR");
                    exception.AddMessage("Invalid correlationHeader, id is required.");
                    throw exception;
                }

                context.TrySetValues(correlationId,
                                     (string)parsedHeader.sourceId,
                                     (string)parsedHeader.sourceName,
                                     (string)parsedHeader.instanceId,
                                     (string)parsedHeader.instanceName,
                                     (string)parsedHeader.userId,
                                     (string)parsedHeader.ipAddress,
                                     correlationHeader);
            }
            catch (FormatException)
            {
                _logger.LogWarning("Invalid correlationheader " + correlationHeader);
                var exception = new ValidationException("Invalid correlationHeader.", "REQCOR");
                exception.AddMessage("Invalid correlationHeader, id is required.");
                throw exception;
            }
        }
        public static void SetDgpHeader(this CorrelationContext context)
        {
            if (context == null)
            {
                throw new NullReferenceException($"{nameof(context)} cannot be null.");
            }

            context.DgpHeader = Convert.ToBase64String(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(context)));
        }
示例#3
0
        public CorrelationContext GetContext()
        {
            var request      = _httpContextAccessor?.HttpContext?.Request;
            var headerValues = request?.Headers?.FirstOrDefault(x => x.Key == CorrelationHeader.Key).Value;

            if (request == null)
            {
                _logger.LogDebug($"{GetType().Name}.CorrelationContext - No incoming request");
            }
            if (!headerValues.HasValue || !headerValues.Value.Any())
            {
                _logger.LogDebug($"{GetType().Name}.CorrelationContext - No correlation header found in incoming request");
            }
            else if (headerValues.Value.Count > 1)
            {
                _logger.LogError($"{GetType().Name}.CorrelationContext( - Multiple correlation headers found in incoming request");
            }
            else
            {
                string correlationHeader = headerValues.Value.FirstOrDefault();
                var    context           = _correlationContextFormatter.ValidateAndSetPropertiesFromDgpHeader(correlationHeader);
                return(context);
            }

            var correlationContext = new CorrelationContext
            {
                Id           = Guid.NewGuid().ToString(),
                SourceId     = _applicationContext.ApplicationId,
                SourceName   = _applicationContext.ApplicationName,
                InstanceId   = _applicationContext.InstanceId,
                InstanceName = _applicationContext.InstanceName,
                IpAddress    = _httpContextAccessor.HttpContext?.Connection?.RemoteIpAddress?.ToString() ?? String.Empty
            };

            correlationContext.SetDgpHeader();

            _logger.LogDebug($"CorrelationHeader not found, created correlationId with header value '{correlationContext.DgpHeader}'");

            return(correlationContext);
        }