奇怪的LINQ例外(Strange LINQ Exception)

编程入门 行业动态 更新时间:2024-10-27 11:29:13
奇怪的LINQ例外(Strange LINQ Exception)

我对这些 帖子也有类似的问题:我是单独发布的,以提供有关我的问题的其他信息,以及我调试它的步骤。

基本上该网站完全正常工作12个小时,然后突然停止工作。 我将开始在之前工作正常的LINQ查询中抛出奇怪的异常。

我在这篇文章的底部包含了堆栈跟踪。

根据类似SO帖子中的建议,我通过直接从服务器资源管理器中的数据库拖动表来删除并重新制作我的DBML。 比较Git中的旧旧,我发现了一些不同的字段:

在dbml中将varchar(255)的一个实例设置为nchar(10) 两个以前在dbml中没有NOT NULL的varchar(255)实例 db(4)字段以前在dbml中没有NOT NULL

一些意见/说明

该网站工作100%罚款一段时间,然后它只是开始抛出这些错误 我可以通过在IIS中重新启动站点重新启动MSSQL服务器来临时修复它,但问题通常会在第二天发生 我无法弄清楚如何手动复制它 对于某些页面,我可以多次重新加载它们并获得不同但类似的例外情况 我可能会在调用.Count()的行上获得InvalidOperationException ,然后重新加载它并在调用.Any()或.First()的行上获取IndexOutOfRangeExcpetion 虽然异常发生了变化,但无论如何我都会在某些页面上获得例外 - 它们永远不会暂时消失 我正在使用存储库模式,因此我的每个类都有一个带有私有DataContext成员的存储库类 - 这可能是为什么? 该站点具有部分视图,其中包含来自db的导航项,并且这似乎总是正常加载,但是从db加载其他内容失败。 也许这个块被缓存了,这就是为什么它继续工作?

似乎总是抛出这条线:

System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539

有关为什么会发生这种情况以及如何解决问题的任何想法?

编辑1:我在相应的堆栈跟踪上面添加了相关代码。 当最后一行代码执行时,异常会被抛出。

这里有一些堆栈跟踪我能够在重新启动站点之前进行copypaste:

IEnumerable<Chapter> toc = (from c in db.Chapters where c.Sections.Any() orderby c.ChapterNumber ascending select c); return toc.Count(); [InvalidOperationException: Sequence contains more than one element] System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500 System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +41 System.Linq.Queryable.Count(IQueryable`1 source) +352 MvcPaging.PagedList`1.Initialize(IQueryable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:63 MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:16 MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:10 MvcPaging.PagingExtensions.ToPagedList(IEnumerable`1 source, Int32 pageIndex, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagingExtensions.cs:77 MyNamespace.Controllers.ChaptersController.TableOfContents(Nullable`1 page) in C:\path\to\my\project\Controllers\ChaptersController.cs:73 lambda_method(Closure , ControllerBase , Object[] ) +118 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976 System.Web.Mvc.Controller.ExecuteCore() +159 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +37 var contentTop = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-top"); var contentBot = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-bottom"); ViewData["ContentAboveTheBox"] = (contentTop.Any() ? contentTop.First().HTML : String.Empty); [IndexOutOfRangeException: Index was outside the bounds of the array.] System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500 System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +50 System.Linq.Queryable.First(IQueryable`1 source) +383 MyNamespace.Controllers.AccountController.SetLoginContentAreas() in C:\path\to\my\project\Controllers\AccountController.cs:112 MyNamespace.Controllers.AccountController.LogIn(String userName, String password, Nullable`1 rememberMe, String returnUrl, Nullable`1 IsEnc) in C:\path\to\my\project\Controllers\AccountController.cs:122 MyNamespace.Controllers.AccountController.LoginPassThru() in C:\path\to\my\project\Controllers\AccountController.cs:82 lambda_method(Closure , ControllerBase , Object[] ) +79 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976 System.Web.Mvc.Controller.ExecuteCore() +159 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

在一个特定页面上还抛出了InvalidCastException 。 更新dbml似乎没有解决这个问题,但是重新启动了站点。

I have a similar issue to these posts: I'm posting separately to provide additional information about the issue in my case, along with steps I've taken to debug it.

Basically the site works completely fine for 12+ hours and then suddenly stops working. I'll start getting strange exceptions thrown in LINQ queries that have previously worked fine.

I have included stack traces at the bottom of this post.

Taking the advice found on similar SO posts, I deleted and remade my DBML by dragging the tables directly from the DB in Server Explorer. Comparing the old and the new in Git, I found a few fields that differed:

One instance of a varchar(255) being set as an nchar(10) in the dbml Two instances of varchar(255) that previously didn't have NOT NULL in the dbml An int(4) field previously didn't have NOT NULL in the dbml

Some observations/notes:

The site works 100% fine for a while and then it just starts throwing these errors I can temporarily fix it by restarting the site in IIS or by restarting MSSQL server, but the problem usually comes back the next day I cannot figure out how to manually replicate it For some pages, I can reload them multiple times and get different but similar exceptions I may get a InvalidOperationException on a line that calls .Count(), then reload it and get a IndexOutOfRangeExcpetion on a line that calls .Any() or .First() Although the exception changes, I always get an exception on certain pages no matter what - they never disappear temporarily I'm using the repository pattern, so each of my classes have a repository class with a private DataContext member - could this be why? The site has a partial view with navigation items which comes from the db, and this always seems to load fine, but loading other things from the db fails. Maybe this block is cached and that's why it continues to work?

The exception always seems to be thrown at this line:

System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539

Any thoughts on why this happening and how to resolve the issue?

EDIT 1: I have added the relevant code above the corresponding stack traces. The exception gets throw when the last line of code executes.

Here are a couple stack traces I was able to copypaste before restarting the site:

IEnumerable<Chapter> toc = (from c in db.Chapters where c.Sections.Any() orderby c.ChapterNumber ascending select c); return toc.Count(); [InvalidOperationException: Sequence contains more than one element] System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500 System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +41 System.Linq.Queryable.Count(IQueryable`1 source) +352 MvcPaging.PagedList`1.Initialize(IQueryable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:63 MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize, Nullable`1 totalCount) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:16 MvcPaging.PagedList`1..ctor(IEnumerable`1 source, Int32 index, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagedList.cs:10 MvcPaging.PagingExtensions.ToPagedList(IEnumerable`1 source, Int32 pageIndex, Int32 pageSize) in C:\path\to\my\project\MvcPaging\src\MvcPaging\PagingExtensions.cs:77 MyNamespace.Controllers.ChaptersController.TableOfContents(Nullable`1 page) in C:\path\to\my\project\Controllers\ChaptersController.cs:73 lambda_method(Closure , ControllerBase , Object[] ) +118 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976 System.Web.Mvc.Controller.ExecuteCore() +159 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +37 var contentTop = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-top"); var contentBot = (new PageRepository()).FindAllPages().Where(p => p.Slug == "login-bottom"); ViewData["ContentAboveTheBox"] = (contentTop.Any() ? contentTop.First().HTML : String.Empty); [IndexOutOfRangeException: Index was outside the bounds of the array.] System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult) +4539 System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries) +207 System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query) +500 System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute(Expression expression) +50 System.Linq.Queryable.First(IQueryable`1 source) +383 MyNamespace.Controllers.AccountController.SetLoginContentAreas() in C:\path\to\my\project\Controllers\AccountController.cs:112 MyNamespace.Controllers.AccountController.LogIn(String userName, String password, Nullable`1 rememberMe, String returnUrl, Nullable`1 IsEnc) in C:\path\to\my\project\Controllers\AccountController.cs:122 MyNamespace.Controllers.AccountController.LoginPassThru() in C:\path\to\my\project\Controllers\AccountController.cs:82 lambda_method(Closure , ControllerBase , Object[] ) +79 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +264 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +129 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +784922 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +314 System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +784976 System.Web.Mvc.Controller.ExecuteCore() +159 System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +335 System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +62 System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +20 System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +54 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +453 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +371

There was also an InvalidCastException thrown on one particular page. Updating the dbml did not seem to fix this, but restarting the site did.

最满意答案

你是否在每个请求上处理你的DataContext(你应该)? 或者你有一个永远存在的DataContext(听起来可能就是这种情况)?

听起来这可能是一个线程问题。

Are you disposing your DataContext on each request (you should be)? Or do you have a single DataContext that lives forever (it sounds like this may be the case)?

It sounds like it could be a threading problem.

更多推荐

本文发布于:2023-08-05 03:18:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1426179.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:奇怪   LINQ   Exception   Strange

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!