【问题标题】:Request timeout for HttpClientHttpClient 的请求超时
【发布时间】:2021-03-03 10:22:15
【问题描述】:

我正在使用带有 ASP.NET Core Web API 的 TypeScript。

场景:

我正在使用后端的大型数据响应服务。加载数据需要两分钟多的时间。据我所知,HttpClient 的默认超时时间为两分钟,localhost (Chrome hold for request time out),但是当我将代码发布到 IIS 站点时,两分钟后它给出了 500 Internal Server Error

在 API 方面:

我已将时间设置为 1000 秒。

 services.AddDbContext<BusinessContext>(options =>
     options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"], opts => opts.CommandTimeout(1000).EnableRetryOnFailure()));

在网页端:

尝试 1:

这是一个简单的服务调用

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp)
      .toPromise() as PacketSearchItem[];
}

试试:2

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp).pipe(timeout(10000000))
      .toPromise() as PacketSearchItem[];
}

尝试:3

async getData(lookUp: Lookup): Promise<PacketSearchItem[]> {
    return await this.http.post(environment.apiBaseUrl + "Packet/ManualSearch", lookUp, { headers: new HttpHeaders({ timeout: `${10000000}` })}).pipe(timeout(10000000))
      .toPromise() as PacketSearchItem[];
}

Google 研究:

Reference

我使用了上面的链接代码,但它给了我同样的错误,因为 2 分钟后谷歌浏览器中出现请求超时。

错误截图:

1) 控制台错误:

2.) 网络错误:

我想等待 Chrome 从 API 获得响应。

我已经添加了 CORS 政策

 services.AddCors(options => {
     options.AddPolicy("CorsPolicy",
         builder => builder.AllowAnyOrigin()
         .AllowAnyMethod()
         .AllowAnyHeader()
         .AllowCredentials());
});

app.UseExceptionHandler(
    builder => {
        builder.Run(
            async context => {
                context.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
                context.Response.Headers.Add("Access-Control-Allow-Origin", "*");

                var error = context.Features.Get<IExceptionHandlerFeature>();
                if (error != null) {
                    context.Response.Headers.Add("Application-Error", error.Error.Message);
                    // CORS
                    context.Response.Headers.Add("access-control-expose-headers", "Application-Error");

                    await context.Response.WriteAsync(error.Error.Message).ConfigureAwait(false);
                }
            });
    });

【问题讨论】:

    标签: node.js typescript http asp.net-core httpclient


    【解决方案1】:

    经过大量研究,我找到了答案。这是 ASP.NET Core 配置文件中的问题:

    web.config

     <aspNetCore requestTimeout="00:20:00">
    

    我们必须在这里增加超时时间。

    帮助链接:

    Reference

    【讨论】:

      【解决方案2】:

      根据您的屏幕截图,这不是超时问题,而是cross-origin resource sharing (CORS) 问题。

      您可能应该花一些时间了解CORS 以及如何使用enable CORS in ASP.NET Core

      【讨论】:

      • 不,这不是 CORS 问题我已经在我的 api 中定义了它,请参阅更新问题。我的整个项目运行良好,问题在于响应时间超过 2 分钟。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-06-17
      • 2019-03-31
      • 1970-01-01
      • 1970-01-01
      • 2011-02-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多