Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC + LINQ exception

I've got a ASP.NET MVC application that works great for 99.9% of the time. Once in a blue moon though things go really wrong and I was wondering if anybody could shed some light on what might go wrong here.

The web application is using Linq2SQL and blows up in a controller after the following set of instructions:

const int pageSize = 5;
var allHeadings = artRepository.FindAllVisibleHeadings();
var paginatedHeadings = new PaginatedList<Article>(allHeadings, id ?? 0, pageSize);

allHeadings contains just an IQueryable list of all visible headings for articles, whereas PaginatedList takes care of taking an appropriate chunk out of this very long list. It's as follows:

public PaginatedList(IQueryable<T> source, int pageIndex, int pageSize)
{
    PageIndex = pageIndex;
    PageSize = pageSize;
    TotalCount = source.Count();
    TotalPages = (int)Math.Ceiling(TotalCount / (double)PageSize);

    this.AddRange(source.Skip(PageIndex * PageSize).Take(PageSize));
}

It blows up on the source.Count() line so when it's counting all the visible articles in the db. Funny thing though is that when I reload the page multiple times I'm getting 2 different kinds of exceptions:

THE FIRST: Sequence contains more than one element

at System.Data.Linq.SqlClient.SqlProvider.Execute(Expression query, QueryInfo queryInfo, IObjectReaderFactory factory, Object[] parentArgs, Object[] userArgs, ICompiledSubQuery[] subQueries, Object lastResult)
at System.Data.Linq.SqlClient.SqlProvider.ExecuteAll(Expression query, QueryInfo[] queryInfos, IObjectReaderFactory factory, Object[] userArguments, ICompiledSubQuery[] subQueries)
at System.Data.Linq.SqlClient.SqlProvider.System.Data.Linq.Provider.IProvider.Execute(Expression query)
at System.Data.Linq.DataQuery`1.System.Linq.IQueryProvider.Execute[S](Expression expression)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
at KoscierzynaInfo.Helpers.PaginatedList`1..ctor(IQueryable`1 source, Int32 pageIndex, Int32 pageSize) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Helpers\PaginatedList.cs:line 20
at KoscierzynaInfo.Controllers.HomeController.Index(Nullable`1 id) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Controllers\HomeController.cs:line 63
at lambda_method(ExecutionScope , 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.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

AND THE SECOND TYPE: Index was outside of the bounds of the array

at System.Data.SqlClient.SqlDataReader.ReadColumnHeader(Int32 i)
at System.Data.SqlClient.SqlDataReader.IsDBNull(Int32 i)
at Read_Article(ObjectMaterializer`1 )
at System.Data.Linq.SqlClient.ObjectReaderCompiler.ObjectReader`2.MoveNext()
at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
at KoscierzynaInfo.Helpers.PaginatedList`1..ctor(IQueryable`1 source, Int32 pageIndex, Int32 pageSize) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Helpers\PaginatedList.cs:line 20
at KoscierzynaInfo.Controllers.HomeController.Index(Nullable`1 id) in C:\Users\mr\Documents\Visual Studio 2008\Projects\KoscierzynaInfo\KoscierzynaInfo\Controllers\HomeController.cs:line 63
at lambda_method(ExecutionScope , 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.ControllerActionInvoker.<>c__DisplayClassa.<InvokeActionMethodWithFilters>b__7()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClassa.<>c__DisplayClassc.<InvokeActionMethodWithFilters>b__9()
at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
at System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName)
at System.Web.Mvc.Controller.ExecuteCore()
at System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext)
at System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContextBase httpContext)
at System.Web.Mvc.MvcHandler.ProcessRequest(HttpContext httpContext)
at System.Web.Mvc.MvcHandler.System.Web.IHttpHandler.ProcessRequest(HttpContext httpContext)
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

The only way I came up with to fix this problem was restarting IIS or recycling the pool. Did this issue happen to any of you before? Where does it come from?! And is there any remedy for that?

I moved the application from IIS7 + SQL Server 2005 to another server with IIS6 + SQLServer 2008 hoping that it would solve the problem, but unfortunately it happened again today, which leads me to believe that this issue is not really system/db dependent.

like image 413
Michal Rogozinski Avatar asked Aug 20 '09 22:08

Michal Rogozinski


3 Answers

I guess the DataContext classes you may be using are tied to a life scope at the request level or at a singleton level. If it is I may suggest you to keep the scope at the method level.

Strange LINQ Exception (See the comments of the answer)

like image 77
VJAI Avatar answered Oct 12 '22 15:10

VJAI


Since you're using IQueryable<T> as source that means that the data are loaded into source parameter only when you call Count() on it.

You can get the first error when calling Single() on a collection with more than one element like this:

var data = Enumerable.Range(1, 10).Single();

Given the above, I might say that somewhere in your code you call Single() and in most of the cases the collection has only one element but when it hase more you get the error.

For the second error: looking at the top of the stack trace you see

SqlDataReader.ReadColumnHeader(Int32 i)

which is the method that throws ArgumentOutOfRangeException. This may indicate that your database model is not synchronized with your database schema or the output of your queries i.e. you're expecting to read N columns but instead you have N-K columns.

like image 38
RePierre Avatar answered Oct 12 '22 15:10

RePierre


The only time I've ever seen error one is when i'm trying to set a single object when more than one object was returned.

if (source == null)
{
  source = new List<T>().AsQueryable();
}

Have you tried including something like this in your pager?

like image 1
griegs Avatar answered Oct 12 '22 13:10

griegs