如何在C#中设置超时时间限制 [英] How to set time limit for timeout in C#

查看:1955
本文介绍了如何在C#中设置超时时间限制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以搜索功能的微服务。



在数据访问层,我们使用Entity框架与DB对话。



我们有数百万的数据库中的记录,对于特定的搜索关键字,由于查询需要时间来呈现实体上下文引发超时异常的数据,因此需要更多时间(约110秒)。



内部例外是:

InnerException:{

消息:发生错误。,

ExceptionMessage:超时已过期。操作完成之前超时时间已过,或者服务器没有响应。,

ExceptionType:System.Data.SqlClient.SqlException,

StackTrace:在System.Threading.Tasks上的System.Data.SqlClient.SqlCommand。<> c。< executedbdatareaderasync> b__167_0(任务`1结果)\\\\ n System.Threading.Tasks.Task.Execute()\\\\ n中的.ContinuationResultTaskFromResultTask`2.InnerInvoke()\\\\ n ---栈尾传输e。在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)\\\\ n中,抛出异常的前一个位置 - \\\\ n )\\\ at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition。< executestorecommandsasync> d__c.MoveNext(),

InnerException:{

消息:发生错误。,

ExceptionMessage:等待操作超时,

异常类型:系统.ComponentModel.Win32Exception,

StackTrace:null

}



所以我的问题是怎么做我让代码等到我收到DB的回复。

是否有任何网页配置我可以添加,以便我可以让代码等待?



非常感谢任何建议/解决方案。



我尝试过:



我尝试将连接字符串中的连接超时参数设置为3000秒。

连接超时= 120;

I have a microsservice which does search functionality.

In the Data access layer we use Entity framework to speak with DB.

We have millions of records in the DB, for a particular search keyword it takes more time (approx - 110 seconds) since the query takes time to render the data the entity context throws an timed out exception.

The Inner Exception is :
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding.",
"ExceptionType": "System.Data.SqlClient.SqlException",
"StackTrace": " at System.Data.SqlClient.SqlCommand.<>c.<executedbdatareaderasync>b__167_0(Task`1 result)\r\n at System.Threading.Tasks.ContinuationResultTaskFromResultTask`2.InnerInvoke()\r\n at System.Threading.Tasks.Task.Execute()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.<executestorecommandsasync>d__c.MoveNext()",
"InnerException": {
"Message": "An error has occurred.",
"ExceptionMessage": "The wait operation timed out",
"ExceptionType": "System.ComponentModel.Win32Exception",
"StackTrace": null
}

So my question is how do i make the code to wait until i get a response from DB.
Is there any web configuration i can add such that i can make the code to wait?

Any suggestions/solutions is highly appreciated.

What I have tried:

I have tried setting the Connection Timeout paramter in connection string to 3000 seconds.
Connection Timeout=120;

推荐答案

连接超时控制应用程序等待建立数据库连接的时间。它对应用程序等待数据库查询执行的时间没有影响。



您需要更改 context.Database.CommandTimeout 属性改为:

Database.CommandTimeout属性(System.Data.Entity) [ ^ ]
The connection timeout controls how long your application will wait for a database connection to be established. It has no effect on how long the application will wait for a database query to execute.

You need to change the context.Database.CommandTimeout property instead:
Database.CommandTimeout Property (System.Data.Entity)[^]


这篇关于如何在C#中设置超时时间限制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆