ASP.NET MVC4和实体框架上的超时问题

时间:2016-08-17 19:31:12

标签: c# asp.net-mvc entity-framework asp.net-mvc-4

我在一个ASP.NET MVC应用程序中遇到以下超时错误。下面是堆栈跟踪。

奇怪的是,用户在仅仅大约5秒后点击调用LINQ查询的按钮就会收到错误。但如果她第二次尝试点击按钮,大约一分钟后,报告就会出现,一切正常。

第一次尝试仅5秒钟后出现错误的事实告诉我这不是超时问题......或者是这样吗? LINQ查询有点复杂,但是当她第二次单击按钮时它可以正常工作。有什么建议吗?

以下是导致超时的代码(LINQ查询):

    private List<ProductionReportExcelWriter.ProductLine> GetProductList(ProductionReporViewInput input)
    {
        return _context.Set<Product>()
            .Include(p => p.Dough)
            .Include(oi => oi.OrderItems)
            .Include(oi => oi.OrderItems.Select(o => o.Order))
            .Include(oi => oi.OrderItems.Select(o => o.Order.Client))
            .AsQueryable()
            .ForNextOrderDate(input.ReportDate)
            .WithActiveClients()
            .ForOrdersItemsWithQuantities()
            .WithoutHiddenProducts().ToList()
            .Select(p => new ProductionReportExcelWriter.ProductLine
            {
                ProductId = p.Id,
                Dough = p.Dough.Name,
                ProductCode = p.ProductCode,
                DoughPosition = p.Dough.Position,
                Description = p.Description,
                Weight = p.Weight,
                Account = p.Account,
            }).ToList();
    }

错误讯息:

  

System.Data.Entity.Core.EntityCommandExecutionException:执行命令定义时发生错误。有关详细信息,请参阅内部异常

     

System.Data.SqlClient.SqlException:超时已过期。操作完成之前经过的超时时间或服务器没有响应。

     

System.ComponentModel.Win32Exception:等待操作超时

完成堆栈跟踪:

System.Data.Entity.Core.EntityCommandExecutionException: An error occurred while executing the command definition. See the inner exception for details. 
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.SqlInternalConnection.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)  
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method, TaskCompletionSource`1 completion, Int32 timeout, Task& task, Boolean asyncWrite)  
at System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method)  
at System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior, String method)  
at System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior behavior)  
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)  
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.<Reader>b__c(DbCommand t, DbCommandInterceptionContext`1 c)  
at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext,TResult](TTarget target, Func`3 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)  
at System.Data.Entity.Infrastructure.Interception.DbCommandDispatcher.Reader(DbCommand command, DbCommandInterceptionContext interceptionContext)  
at System.Data.Entity.Internal.InterceptableDbCommand.ExecuteDbDataReader(CommandBehavior behavior)  
at System.Data.Common.DbCommand.ExecuteReader(CommandBehavior behavior)  
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)  
--- End of inner exception stack trace ---  
at System.Data.Entity.Core.EntityClient.Internal.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior behavior)  
at System.Data.Entity.Core.Objects.Internal.ObjectQueryExecutionPlan.Execute[TResultType](ObjectContext context, ObjectParameterCollection parameterValues)  
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__2()  
at System.Data.Entity.Core.Objects.ObjectContext.ExecuteInTransaction[T](Func`1 func, IDbExecutionStrategy executionStrategy, Boolean startLocalTransaction, Boolean releaseConnectionOnSuccess)  
at System.Data.Entity.Core.Objects.ObjectQuery`1.<>c__DisplayClass3.<GetResults>b__1()  
at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)  
at System.Data.Entity.Core.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)  
at System.Data.Entity.Core.Objects.ObjectQuery`1.<System.Collections.Generic.IEnumerable<T>.GetEnumerator>b__0()  
at System.Data.Entity.Internal.LazyEnumerator`1.MoveNext()  
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)  
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)  
at Arhoma.Core.Views.ProductionReportViewFactory.GetProductList(ProductionReporViewInput input) in C:\Projects\webapp-commande\core-commande\Views\ProductionReportViewFactory.cs:line 66
at Arhoma.Core.Views.ProductionReportViewFactory.CreateProductionReportExcelWriterModel(ProductionReporViewInput input) in C:\Projects\webapp-commande\core-commande\Views\ProductionReportViewFactory.cs:line 37
at Arhoma.Core.Views.ProductionReportViewFactory.Load(ProductionReporViewInput input) in C:\Projects\webapp-commande\core-commande\Views\ProductionReportViewFactory.cs:line 26
at Arhoma.Core.ViewRepositoryOutputBuilder`1.Using[TInput](TInput input) in C:\Projects\webapp-commande\core-commande\ViewRepository.cs:line 42
at Arhoma.WebApp.Controllers.ClosingController.ProductionReport(Nullable`1 date) in C:\Projects\webapp-commande\webapp-commande\Controllers\ClosingController.cs:line 47
at lambda_method(Closure , ControllerBase , Object[] )
at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass42.<BeginInvokeSynchronousActionMethod>b__41()
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<>c__DisplayClass2a.<BeginInvokeAction>b__20()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult)
at System.Web.Mvc.Async.AsyncResultWrapper.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar)
at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`1.End()
at System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult)
at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

1 个答案:

答案 0 :(得分:0)

在其中写入连接字符串标记的web.config文件中,增加超时,如Timeout = 200或Timeout = 500。  例如 -

<add name="ConnectionString" connectionString="Data Source=;Initial Catalog=;Persist Security Info=True;User ID=sa;Password=sa@123;Connect Timeout=200" providerName="System.Data.SqlClient"/>