コード例 #1
0
        /// <summary>
        /// Processes the HTTP requests
        /// </summary>
        /// <param name="context">The HTTP Context associated with the request</param>
        public async Task Invoke(HttpContext context)
        {
            var processed = false;

            foreach (var pair in _configuration.EndpointList)
            {
                if (string.Equals(pair.Value, context.Request.Path, StringComparison.OrdinalIgnoreCase))
                {
                    try
                    {
                        _logger.LogInformation("Validation endpoint found.");

                        if (await _validator.ValidateAsync(pair.Key, pair.Value, ContainerLifecycle.CancellationToken).ConfigureAwait(true))
                        {
                            context.Response.StatusCode  = (int)HttpStatusCode.OK;
                            context.Response.ContentType = JsonContentType;
                            await context.Response.WriteAsync(JsonSuccessResponse, ContainerLifecycle.CancellationToken).ConfigureAwait(true);
                        }
                        else
                        {
                            context.Response.StatusCode  = (int)HttpStatusCode.NotFound;
                            context.Response.ContentType = JsonContentType;
                            await context.Response.WriteAsync(JsonFailureResponse, ContainerLifecycle.CancellationToken).ConfigureAwait(true);
                        }
                    }
                    catch (Exception e)
                    {
                        _logger.LogError(e, "Error while executing validation logic");
                    }

                    processed = true;
                    continue;
                }
            }

            if (!processed && _next != null)
            {
                if (await _validator.UnknownEndpointAsync(context.Request.Path, ContainerLifecycle.CancellationToken).ConfigureAwait(true))
                {
                    await _next.Invoke(context).ConfigureAwait(false);
                }
                else
                {
                    context.Response.StatusCode  = (int)HttpStatusCode.BadRequest;
                    context.Response.ContentType = JsonContentType;
                    await context.Response.WriteAsync(JsonFailureResponse, ContainerLifecycle.CancellationToken).ConfigureAwait(true);
                }
            }
        }