C# 为什么尽管在appsettings中设置了一个较大的超时值,但仍会得到查询超时
我有一个数据提供者类。下面是它的简化版本C# 为什么尽管在appsettings中设置了一个较大的超时值,但仍会得到查询超时,c#,asp.net-core,entity-framework-core,asp.net-core-1.0,asp.net-core-webapi,C#,Asp.net Core,Entity Framework Core,Asp.net Core 1.0,Asp.net Core Webapi,我有一个数据提供者类。下面是它的简化版本 namespace EmployeeManager { public class EmployeeDataProvider : IEmployeeDataProvider { private DataObjects.EmployeeServiceContext databaseContext; IDbContextTransaction transaction; public Emplo
namespace EmployeeManager
{
public class EmployeeDataProvider : IEmployeeDataProvider
{
private DataObjects.EmployeeServiceContext databaseContext;
IDbContextTransaction transaction;
public EmployeeDataProvider(DataObjects.EmployeeServiceContext context)
{
this.databaseContext = context;
}
public void CreateEmployee(Id employeeId, string description)
{
transaction = databaseContext.Database.BeginTransaction();
var employeeDto = databaseContext.Employee.Where(x => x.EmployeeId == employeeId).FirstOrDefault();
if (employeeDto == null)
{
databaseContext.Employee.Add(new DataObjects.Employee() { EmployeeId = employeeId, Description = description });
}
}
public void AddOrUpdateEmployeeData(EmployeeData data)
{
// Add or updates multiple Employee related tables
}
public void CommitChanges()
{
if (databaseContext.ChangeTracker.HasChanges())
databaseContext.SaveChanges();
transaction.Commit();
transaction.Dispose();
}
public void RollbackChanges()
{
if (transaction != null)
{
transaction.Rollback();
transaction.Dispose();
}
}
}
}
多个客户端可以调用使用此提供程序的相关服务。它的设置在Startup.cs中如下所示
public class Startup
{
public Startup(IHostingEnvironment env)
{
.....
}
public IConfigurationRoot Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
......
services.AddTransient(typeof(IEmployeeDataProvider), typeof(EmployeeDataProvider));
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseStaticFiles();
app.UseDeveloperExceptionPage();
app.UseMvc();
}
}
在不同的加载条件下,我在日志文件中看到了很多以下内容。请注意,没有堆栈跟踪指向我的类中的任何方法
Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory|An exception occurred in the database while iterating the results of a query.
System.Data.SqlClient.SqlException: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. ---> System.ComponentModel.Win32Exception: The wait operation timed out
--- End of inner exception stack trace ---
at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)
at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)
at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady)
at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
at System.Data.SqlClient.SqlDataReader.get_MetaData()
at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior runBehavior, String resetOptionsString)
at System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, Boolean async, Int32 timeout, Task& task, Boolean asyncWrite, SqlDataReader ds)
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, String executeMethod, IReadOnlyDictionary`2 parameterValues, Boolean closeConnection)
at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMoveNext(Boolean buffer)
at Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func`2 operation, Func`2 verifySucceeded, TState state)
at Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.<_ShapedQuery>d__3`1.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.<_TrackEntities>d__15`2.MoveNext()
at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
Microsoft.EntityFrameworkCore.Query.Internal.SqlServerQueryCompilationContextFactory |在迭代查询结果时,数据库中发生异常。
System.Data.SqlClient.SqlException:超时已过期。操作完成前的超时时间或服务器没有响应。-->System.ComponentModel.Win32异常:等待操作超时
---内部异常堆栈跟踪的结束---
位于System.Data.SqlClient.SqlConnection.OneError(SqlException异常,布尔断开连接,操作'1 wrapCloseInAction)
位于System.Data.SqlClient.TdsParser.ThroweException和Warning(TdsParserStateObject StateObjectStateObj、布尔调用方连接锁、布尔异步关闭)
位于System.Data.SqlClient.TdsParser.TryRun(RunBehavior RunBehavior、SqlCommand cmdHandler、SqlDataReader dataStream、BulkCopySimpleResultSet bulkCopyHandler、TdsParserStateObject stateObj、Boolean和dataReady)
位于System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()处
在System.Data.SqlClient.SqlDataReader.get_MetaData()处
位于System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds、RunBehavior、String ResetOptions String)
位于System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior、RunBehavior RunBehavior、Boolean returnStream、Boolean async、Int32超时、任务和任务、Boolean asyncWrite、SqlDataReader ds)
位于System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior)
在Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection连接、String executeMethod、iRelationalYDictionary`2参数值、布尔closeConnection)
位于Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteReader(IRelationalConnection连接,IReadOnlyDictionary`2参数值)
位于Microsoft.EntityFrameworkCore.Query.Internal.QueryingEnumerable.Enumerator.BufferlessMovenText(布尔缓冲区)
在Microsoft.EntityFrameworkCore.Storage.Internal.SqlServerExecutionStrategy.Execute[TState,TResult](Func`2操作,Func`2验证成功,TState)
在Microsoft.EntityFrameworkCore.Query.QueryMethodProvider.d_u3`1.MoveNext()中
在Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.d_u15`2.MoveNext()中
位于Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
appsettings.json中我的连接字符串如下所示
“AppConnectionString”:“服务器=MyPC;用户”
ID=myuser;密码=mypassword;数据库=myDb;连接超时=2000“
错误发生在一分钟之内,所以它不像查询等待2000秒以等待查询超时。我需要在代码中的某个地方设置超时吗?你能显示你的实际代码吗?我个人会在命令对象中设置超时,你在方法或函数中声明的超时。实际代码很大,我会尝试将其修剪掉,但在我的场景中,我没有在任何地方设置任何命令超时。它是通过dbcontext.Database.SetCommandTimout()设置的吗?再加上多个请求正在命中这些方法,因此,超过500个客户端可以同时调用相同的方法。因此,您应该设置新命令,并且超时。这将纠正您的问题。但是我需要更好地查看您的代码,同时检查此链接。这将是一个更好的选项/解决方案,您可以将其设置为60*2000,例如,在我刚才展示的链接示例中,您没有这样做通过增加超时来对抗超时。改为使代码可扩展。连接超时不是命令/查询超时。找出如何使您的查询更快(甚至尽可能避免执行)