C# .netcore 3.1应用程序在CORS发布1分钟后完全中断
我在.netcore 3.1(WebAPI)中运行一个应用程序作为后端,Angular 9作为前端 我的应用程序中有一个通过CSV文件进行批量用户上传的模块,当我们上传用户时,前端的加载程序会启动,成功上传后会进入下一个屏幕,屏幕上显示“上传成功” 该模块在.netcore2.0中运行时(一周前)在我的应用程序中运行良好 但最近我已经将我的应用程序迁移到.netcore 3.1 我现在面临的问题是,当我尝试上传批量用户时,它在前端恰好在1分钟后中断,并在控制台上抛出CORS问题,并停留在同一屏幕上。但是捕获是在API级别,它工作正常,完成了循环,并向所有添加的用户发送欢迎电子邮件,他们能够登录到应用程序 控制台和网络选项卡中的错误: 此外,当我试图在本地复制相同的问题时,它工作良好,不可复制 我尝试在本地服务器上复制此问题:C# .netcore 3.1应用程序在CORS发布1分钟后完全中断,c#,amazon-s3,cors,asp.net-core-webapi,asp.net-core-3.1,C#,Amazon S3,Cors,Asp.net Core Webapi,Asp.net Core 3.1,我在.netcore 3.1(WebAPI)中运行一个应用程序作为后端,Angular 9作为前端 我的应用程序中有一个通过CSV文件进行批量用户上传的模块,当我们上传用户时,前端的加载程序会启动,成功上传后会进入下一个屏幕,屏幕上显示“上传成功” 该模块在.netcore2.0中运行时(一周前)在我的应用程序中运行良好 但最近我已经将我的应用程序迁移到.netcore 3.1 我现在面临的问题是,当我尝试上传批量用户时,它在前端恰好在1分钟后中断,并在控制台上抛出CORS问题,并停留在同一屏幕
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(CorsPolicy.ALLOW_ALL);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseRouting();
app.UseCors(CorsPolicy.ALLOW_ALL);
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
}
我的CORS政策: a)Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddNewtonsoftJson(a => a.SerializerSettings.NullValueHandling = NullValueHandling.Ignore)
.AddNewtonsoftJson(a => a.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
CorsPolicy.ConfigureServices(services);
ConfigureDependencyInjection(services);
}
public class CorsPolicy
{
public const string ALLOW_ALL = "AllowAll";
public static void ConfigureServices(IServiceCollection services)
{
services.AddCors(AllowAll);
}
private static void AllowAll(CorsOptions options)
{
options.AddPolicy(ALLOW_ALL, builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
}
}
b)CorsPolicy.cs
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc()
.AddNewtonsoftJson(a => a.SerializerSettings.NullValueHandling = NullValueHandling.Ignore)
.AddNewtonsoftJson(a => a.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Utc);
CorsPolicy.ConfigureServices(services);
ConfigureDependencyInjection(services);
}
public class CorsPolicy
{
public const string ALLOW_ALL = "AllowAll";
public static void ConfigureServices(IServiceCollection services)
{
services.AddCors(AllowAll);
}
private static void AllowAll(CorsOptions options)
{
options.AddPolicy(ALLOW_ALL, builder =>
{
builder
.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
});
}
}
c)web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<handlers>
<add requestTimeout="00:20:00" name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
</handlers>
<aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" hostingModel="InProcess">
<environmentVariables>
<environmentVariable name="COMPLUS_ForceENC" value="1" />
<environmentVariable name="ASPNETCORE_ENVIRONMENT" value="Development" />
</environmentVariables>
</aspNetCore>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
</system.webServer>
</configuration>
请提出任何解决方案。提前感谢。此错误的原因之一是web.config中有下面的部分。注释下面的代码并直接在IIS中设置选项 尝试从web.config文件中删除以下部分 另外,请看:
找到了解决方案
首先,我的chrome非常可怜,它没有显示同一请求的504网关超时问题。因为服务器闲置了1分钟。它打破了循环并抛出了CORS错误
有时,CORS的错误是相当误导的
对于解决方案,我更新了AWS服务器中该API的idle timeout属性,与之前的60秒相同。这对我很有效
谢谢大家的帮助
我们无法调试图像。请发布代码。@John用代码更新了图像,如果我没有错,添加中间件对异常日志记录没有意义,我已经实现了代码。但在本例中,它不会记录任何内容。我也更新了我的web.config文件,并且我的web.config中没有此代码。