src/Services/Basket/Basket.API/Infrastructure/Middlewares/FailingMiddleware.cs

(开头部分) 2KB

这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。

namespace Basket.API.Infrastructure.Middlewares;

using Microsoft.Extensions.Logging;

public class FailingMiddleware
{
    private readonly RequestDelegate _next;
    private bool _mustFail;
    private readonly FailingOptions _options;
    private readonly ILogger _logger;

    public FailingMiddleware(RequestDelegate next, ILogger<FailingMiddleware> logger, FailingOptions options)
    {
        _next = next;
        _options = options;
        _mustFail = false;
        _logger = logger;
    }

    public async Task Invoke(HttpContext context)
    {
        var path = context.Request.Path;
        if (path.Equals(_options.ConfigPath, StringComparison.OrdinalIgnoreCase))
        {
            await ProcessConfigRequest(context);
            return;
        }

        if (MustFail(context))
        {
            _logger.LogInformation("Response for path {Path} will fail.", path);
            context.Response.StatusCode = (int)System.Net.HttpStatusCode.InternalServerError;
            context.Response.ContentType = "text/plain";
            await context.Response.WriteAsync("Failed due to FailingMiddleware enabled.");
        }
        else
        {
            await _next.Invoke(context);
        }
    }

    private async Task ProcessConfigRequest(HttpContext context)
    {
        var enable = context.Request.Query.Keys.Any(k => k == "enable");
        var disable = context.Request.Query.Keys.Any(k => k == "disable");

        if (enable && disable)
        {
            throw new ArgumentException("Must use enable or disable querystring values, but not both");
        }

        if (disable)
        {
            _mustFail = false;
            await SendOkResponse(context, "FailingMiddleware disabled. Further requests will be processed.");
            return;
        }
        if (enable)
        {
            _mustFail = true;
后面还有 30 行代码,解锁后查看完整代码

24 小时内免费解锁 3 个项目,之后 1 算力/个。 规则说明

AI 解读 AI

登录后可用,每次 10 算力,解读结果公开显示在下面。

还没有人解读过这个文件。