コード例 #1
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);
        }
コード例 #2
0
        public async Task InvokeAsync(HttpContext context, RequestDelegate next)
        {
            if (context.Request.Method != "POST")
            {
                await next(context);

                return;
            }

            var message = await _payloadBuilder.BuildFromJsonAsync <object>(context.Request);

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

            if (message is JObject jObject)
            {
                jObject.SetResourceId(resourceId);
            }

            context.SetResourceIdFoRequest(resourceId);
            await using var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(message)));
            context.Request.Body         = memoryStream;
            await next(context);
        }