【发布时间】: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 研究:
我使用了上面的链接代码,但它给了我同样的错误,因为 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