public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (!_endpoints.TryGetValue(context.Request.Method, out var endpoints))
            {
                await next(context);
                return;
            }

            foreach (var endpoint in endpoints)
            {
                var match = _routeMatcher.Match(endpoint.Path, context.Request.Path);
                if (match is null)
                {
                    continue;
                }

                var key = $"{endpoint.Exchange}:{endpoint.RoutingKey}";
                if (!Conventions.TryGetValue(key, out var conventions))
                {
                    conventions = new MessageConventions(typeof(object), endpoint.RoutingKey, endpoint.Exchange, null);
                    Conventions.TryAdd(key, conventions);
                }

                var messageId = Guid.NewGuid().ToString("N");
                var correlationId = _correlationIdFactory.Create();
                var resourceId = Guid.NewGuid().ToString("N");
                var correlationContext = _correlationContextBuilder.Build(context, correlationId, default,
Beispiel #2
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (_routes is null || !_routes.Any())
            {
                await next(context);

                return;
            }

            var key = GetKey(context);

            if (!_routes.TryGetValue(key, out var route))
            {
                await next(context);

                return;
            }

            if ((_authenticate && route.Authenticate != false || route.Authenticate == true) &&
                !_anonymousRouteValidator.HasAccess(context.Request.Path))
            {
                var authenticateResult = await context.AuthenticateAsync();

                if (!authenticateResult.Succeeded)
                {
                    context.Response.StatusCode = 401;
                    return;
                }

                context.User = authenticateResult.Principal;
            }

            if (!Conventions.TryGetValue(key, out var conventions))
            {
                conventions = new MessageConventions(typeof(object), route.RoutingKey, route.Exchange, null);
                Conventions.TryAdd(key, conventions);
            }

            var spanContext = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString();
            var message     = await _payloadBuilder.BuildFromJsonAsync <object>(context.Request);

            var resourceId = Guid.NewGuid().ToString("N");

            if (context.Request.Method == "POST" && message is JObject jObject)
            {
                jObject.SetResourceId(resourceId);
            }

            var messageId          = Guid.NewGuid().ToString("N");
            var correlationId      = Guid.NewGuid().ToString("N");
            var correlationContext = _correlationContextBuilder.Build(context, correlationId, spanContext,
                                                                      route.RoutingKey, resourceId);

            _rabbitMqClient.Send(message, conventions, messageId, correlationId, spanContext, correlationContext);
            context.Response.StatusCode = 202;
            context.Response.SetOperationHeader(correlationId);
        }
Beispiel #3
0
        protected async Task SendAsync <T>(T message) where T : class
        {
            var spanContext   = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString();
            var messageId     = Guid.NewGuid().ToString("N"); //this is unique per message type, each message has its own messageId in rabbitmq
            var correlationId = Guid.NewGuid().ToString("N"); //unique for whole message flow , here gateway initiate our correlationId along side our newly publish message to keep track of our request

            var resourceId = Guid.NewGuid().ToString("N");

            if (HttpContext.Request.Method == "POST" && message is JObject jObject)
            {
                jObject.SetResourceId(resourceId);
            }
            var correlationContext = _correlationContextBuilder.Build(HttpContext, correlationId, spanContext, message.GetType().Name.ToSnakeCase(), resourceId);
            await _busPublisher.PublishAsync <T>(message, messageId : messageId, correlationId : correlationId, spanContext : spanContext, messageContext : correlationContext);

            HttpContext.Response.StatusCode = 202; //we send 202 status code and a correlationId immediately to end user after we published message to the message broker
            HttpContext.Response.SetOperationHeader(correlationId);
        }
        protected override Task <HttpResponseMessage> SendAsync(HttpRequestMessage request,
                                                                CancellationToken cancellationToken)
        {
            var httpContext        = _httpContextAccessor.HttpContext;
            var spanContext        = _tracer.ActiveSpan is null ? string.Empty : _tracer.ActiveSpan.Context.ToString();
            var correlationId      = Guid.NewGuid().ToString("N");
            var resourceId         = httpContext.GetResourceIdFoRequest();
            var correlationContext = _correlationContextBuilder.Build(httpContext, correlationId, spanContext,
                                                                      resourceId: resourceId);

            request.Headers.TryAddWithoutValidation("Correlation-Context",
                                                    JsonConvert.SerializeObject(correlationContext));

            if (OperationHeaderRequests.Contains(httpContext.Request.Method))
            {
                httpContext.Response.SetOperationHeader(correlationId);
            }

            return(base.SendAsync(request, cancellationToken));
        }