[英]Failed executing DbCommand because timeout expired .net core
我的目标是提供简单的 API 来从包含 5 列的Payments
(约 400 行)表中检索数据。
Payment: Id (int),
PaymentsNumber (tinyint),
Rate (decimal(18,2)),
ProductType (tinyint),
ClientClubType (tinyint).
用户可以使用请求参数(应返回 ~12 行)发出帖子请求:
PaymentsRequest
{
public int? PaymentsNumber { get; set; }
public byte? ProductType { get; set; }
public byte? ClientClubType { get; set; }
}
使用 EF 核心:
services.AddDbContext<MyContext>(cfg => cfg.UseSqlServer(Configuration.GetConnectionString(...),optionsBuilder => optionsBuilder.CommandTimeout(60)));
public async Task<IEnumerable<Payments>> GetPaymentsAsync(PaymentsRequest request)
{
IQueryable<Payments> query = this._context.Set<Payments>();
query = query.Where(filter =>
(request.ClientClubType == null || filter.ClientClubType == request.ClientClubType) &&
(request.ProductType == null || filter.ProductType == request.ProductType) &&
(request.PaymentsNumber == null || filter.PaymentsNumber == request.PaymentsNumber));
return await query.ToListAsync();
}
在 azure 应用程序洞察中,我可以看到 2 个连续的日志,由相同的异常引起:
Log1 是(虽然这里不需要写 log2):
执行 DbCommand 失败(65,238 毫秒)[Parameters=[@__request_ClientClubType_0='?' (Size = 1) (DbType = Byte), @__request_ProductType_1='?' (Size = 1) (DbType = Byte)], CommandType='Text', CommandTimeout='60']
SELECT [p].[Id], [p].[ClientClubType], [p].[PaymentsNumber], [p].[ProductType], [p].[Rate] FROM [Payments] AS [p] WHERE ( ([p].[ClientClubType] = @__request_ClientClubType_0) AND @__request_ClientClubType_0 不是 NULL) AND (([p].[ProductType] = @__request_ProductType_1) AND @__request_ProductType_1 不是 NULL)
我的应用程序是部署在 azure linux webapp 上的 .net core 3.0 应用程序。
该问题仅在生产中出现,并非每次都发生,我无法从 MSSMS 重建该问题。 任何的想法?
更新:
在@panagiotis-kanavos 发表评论后,我将代码更新为:
services.AddDbContextPool<MyContext>(cfg => cfg.UseSqlServer(Configuration.GetConnectionString(...),optionsBuilder => optionsBuilder.CommandTimeout(60)));
public async Task<IEnumerable<Payments>> GetPaymentsAsync(PaymentsRequest request)
{
IQueryable<Payments> query = this._context.Payments;
query = query.Where(filter =>
(filter.ClientClubType == request.ClientClubType) &&
(filter.ProductType == request.ProductType) &&
(filter.PaymentsNumber == request.PaymentsNumber));
return await query.ToListAsync();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.