Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT 中间件 C# .NET Core 2.2

Posted

技术标签:

【中文标题】Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT 中间件 C# .NET Core 2.2【英文标题】:Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT Middleware C# .NET Core 2.2 【发布时间】:2020-11-27 17:58:26 【问题描述】:

我有一个 .NET Core 2.2 C# API,它随机崩溃并出现以下问题:

2020-08-07 10:44:42.039 +00:00 [Error] Exception occurred while processing message.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
2020-08-07 10:44:42.062 +00:00 [Error] Connection ID ""17798225728978863173"", Request ID ""8000c446-0000-f700-b63f-84710c7967bb"": An unhandled exception was thrown by the application.
System.OperationCanceledException: The operation was canceled.
   at System.Threading.CancellationToken.ThrowOperationCanceledException()
   at System.Threading.SemaphoreSlim.WaitUntilCountOrTimeoutAsync(TaskNode asyncWaiter, Int32 millisecondsTimeout, CancellationToken cancellationToken)
   at Microsoft.IdentityModel.Protocols.ConfigurationManager`1.GetConfigurationAsync(CancellationToken cancel)
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler.HandleAuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.AuthenticationHandler`1.AuthenticateAsync()
   at Microsoft.AspNetCore.Authentication.AuthenticationService.AuthenticateAsync(HttpContext context, String scheme)
   at Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
   at Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
   at Swashbuckle.AspNetCore.SwaggerUI.SwaggerUIMiddleware.Invoke(HttpContext httpContext)
   at Swashbuckle.AspNetCore.Swagger.SwaggerMiddleware.Invoke(HttpContext httpContext, ISwaggerProvider swaggerProvider)
   at Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT`1.ProcessRequestAsync()

我无法在其他环境中重现此问题。这在我看来就像中间件(或者可能是身份验证)中存在问题

我继承了该项目并注意到中间件的顺序可能不正确: 启动.cs:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        
            if (env.IsDevelopment())
            
                app.UseDeveloperExceptionPage();
            

            // swagger
            app.UseSwagger();
            app.UseSwaggerUI(options => options.ConfigureSwaggerUi(this.Configuration));

            // versioning
            app.UseCustomApiVersionHeader(this.Configuration);

            // authentication
            app.UseAuthentication();

            // static files
            app.UseStaticFiles();

            app.UseMvc();
        

我还有一个自定义身份验证服务:

public void ConfigureServices(IServiceCollection services)
 
...
services.AddCustomAuthentication(this.Configuration);
...

身份验证IOC:

namespace Authentication.Api.Middleware

    public static class CustomAuthenticationIoc
    
        public static IServiceCollection AddCustomAuthentication(this IServiceCollection services, IConfiguration configuration)
        
            var domain = $"https://configuration["security:domain"]/";

            services.AddAuthentication(options =>
            
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            ).AddJwtBearer(options =>
            
                options.Authority = $"https://configuration["security:domain"]/";
                options.Audience = configuration["security:audience"];
            );

            // get all policies
            var policies = configuration["security:policies"]?
                .Split(' ', StringSplitOptions.RemoveEmptyEntries)?
                .ToList();

            services.AddAuthorization(configure: options =>
            
                foreach (var policyItem in policies)
                
                    options.AddPolicy(policyItem,
                        policy => policy.Requirements.Add(new HasScopeRequirement(policyItem, domain)));
                
            );

            // register the scope authorization handler
            services.AddSingleton<IAuthorizationHandler, HasScopeHandler>();

            return services;
        
    

任何帮助都将不胜感激,因为我已经探索了许多不同的根源:环境、身份验证和 JWT Swagger 等。

【问题讨论】:

【参考方案1】:

至少告诉你,你并不孤单。我遇到了类似的问题并正在调查

【讨论】:

【参考方案2】:

当客户端(浏览器,应用程序)取消请求时发生,然后您的网络应用程序取消此请求,作业。

【讨论】:

【参考方案3】:

(几个月后编辑...这并没有解决问题)

我几乎可以肯定这是 Microsoft.AspNetCore.Authentication.x 3.1.4 或 3.1.5 包中的错误。自 2020 年 7 月安装这些软件包以来,我很少遇到此错误,从那以后我一直在各处看到它。几周前我升级到 3.1.14,但我已经有一周没有在日志中看到它了。

【讨论】:

欢迎使用 Stack Overflow 线圈。请阅读how to write a good answer 上的指南。目前这并不是问题的真正答案,因为您只是推测它可能已通过更新修复。

以上是关于Microsoft.AspNetCore.Server.IIS.Core.IISHttpContextOfT 中间件 C# .NET Core 2.2的主要内容,如果未能解决你的问题,请参考以下文章