Search Error: InvalidOperationException - "Code supposed to be unreachable" - thrown by System.Core.

Topics: Customizing Orchard, Writing modules
Jul 13, 2012 at 3:12 PM
Edited Jul 13, 2012 at 3:15 PM

Hi

I have the following action on an EmailAdminController in my module.  Essentially, it displays a list of CorporateEmailRecord objects, and it implements searching as shown in the WebShop series of SkyWalker Software (It is a bit different):

public ActionResult Index(PagerParameters pagerParameters, CorporateEmailsSearchVM search) 
        {
            var query = service.QueryCorporateEmails();
 
            // If the user specified a search expression, update the query with a filter
            if (!string.IsNullOrWhiteSpace(search.Expression))
            {
                var expression = search.Expression.Trim();
 
                query = from ce in query
                        where
                            ce.Alias.Contains(expression, StringComparison.InvariantCultureIgnoreCase) ||
                            ce.EmailAddress.Contains(expression, StringComparison.InvariantCultureIgnoreCase)
                        select ce;
            }
 
            // Project the query into a list of customer shapes
            var projection = from ce in query
                             orderby ce.DisplayOrder
                             select ce;
 
            // The pager is used to apply paging on the query and to create a PagerShape
            var pager = new Pager(siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize);
 
            // Apply paging
            var corporateEmails = projection.Skip(pager.GetStartIndex()).Take(pager.PageSize);
 
            // Construct a Pager shape
            var count = projection.Count();
            var pagerShape = Shape.Pager(pager).TotalItemCount(count);
 
            // Create the viewmodel
            var model = new CorporateEmailsIndexVM(corporateEmails.ToList(), search, pagerShape);
 
            return View(model);
        }

This is different from the quoted source, as I was getting an error that basically stated that I couldn't have a dynamic operation in an Expression Tree.  So I made the query plain old strongly typed.

However, the following line is giving me grief, in the form of an obscure error ("Code supposed to be unreachable"):

var count = projection.Count();

I have no idea what is causing this error. The error detail is:

Oops. Something went wrong ... sorry

An unhandled exception has occurred and the request was terminated. Please refresh the page. If the error persists, go back

Code supposed to be unreachable

System.InvalidOperationException: Code supposed to be unreachable at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpressionFreeTemps(Expression expression, Stack stack) at System.Linq.Expressions.Compiler.StackSpiller.Rewrite[T](Expression`1 lambda) at System.Linq.Expressions.Expression`1.Accept(StackSpiller spiller) at System.Linq.Expressions.Compiler.LambdaCompiler.Compile(LambdaExpression lambda, DebugInfoGenerator debugInfoGenerator) at System.Linq.Expressions.LambdaExpression.Compile() at NHibernate.Linq.Util.QueryUtil.GetExpressionValue(Expression expression) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.GetCollectionContainsCriteria(Expression list, Expression containedExpr) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitMethodCall(MethodCallExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitOrElseExpression(BinaryExpression expr) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitBinary(BinaryExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.ExpressionVisitor.VisitLambda(LambdaExpression lambda) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.VisitUnary(UnaryExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.WhereArgumentsVisitor.GetCriterion(ICriteria rootCriteria, ISession session, Expression expression) at NHibernate.Linq.Visitors.RootVisitor.HandleWhereCall(MethodCallExpression call) at NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.RootVisitor.VisitMethodCall(MethodCallExpression expr) at NHibernate.Linq.Visitors.ExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateExpressionVisitor.Visit(Expression exp) at NHibernate.Linq.Visitors.NHibernateQueryTranslator.TranslateInternal(Expression expression) at NHibernate.Linq.Visitors.NHibernateQueryTranslator.Translate(Expression expression, QueryOptions queryOptions) at NHibernate.Linq.NHibernateQueryProvider.TranslateExpression(Expression expression) at NHibernate.Linq.NHibernateQueryProvider.Execute(Expression expression) at NHibernate.Linq.QueryProvider.System.Linq.IQueryProvider.Execute[T](Expression expression) at System.Linq.Queryable.Count[TSource](IQueryable`1 source) at Wingspan.CorporateEmails.Controllers.EmailAddressesAdminController.Index(PagerParameters pagerParameters, CorporateEmailsSearchVM search) 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.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)


 

Note:

The initial line in my action code:

var query = service.QueryCorporateEmails();

This call to a service just returns an IQueryable<CorporateEmailRecord> from an injected IRepository<CorporateEmailRecord>:

 

public IQueryable<CorporateEmailRecord> QueryCorporateEmails() {
            return _repositoryCorporteEmailRecord.Table;
        }
Coordinator
Jul 13, 2012 at 3:57 PM

I'd bet this is what makes nHib choke: 

ce.Alias.Contains
Jul 13, 2012 at 5:41 PM

Bertrand

You are probably right, I will double check that I have a reference to that extension method.  It will be monday now, though.

Coordinator
Jul 13, 2012 at 5:44 PM

It's not a question of having a reference to it: it's that nHibernate doesn't support everything you can do with Linq.

Jul 17, 2012 at 12:29 PM
Edited Jul 17, 2012 at 12:30 PM

Bertrand

You were right, it was nHib choking on the Contains extension method.  If I do the following:

public ActionResult Index(PagerParameters pagerParameters, CorporateEmailsSearchVM search) 
        {
            var query = service.QueryCorporateEmails().ToList();
 
            // If the user specified a search expression, update the query with a filter
            if (!string.IsNullOrWhiteSpace(search.Expression))
            {
                var expression = search.Expression.Trim();
 
                query = (from ce in query
                        where
                            ce.Alias.Contains(expression, StringComparison.InvariantCultureIgnoreCase) ||
                            ce.EmailAddress.Contains(expression, StringComparison.InvariantCultureIgnoreCase)
                        select ce).ToList();
            }
 
            // Project the query into a list of customer shapes
            var projection = from ce in query
                             orderby ce.DisplayOrder
                             select ce;
 
            // The pager is used to apply paging on the query and to create a PagerShape
            var pager = new Pager(siteService.GetSiteSettings(), pagerParameters.Page, pagerParameters.PageSize);
 
            // Apply paging
            var corporateEmails = projection.Skip(pager.GetStartIndex()).Take(pager.PageSize);
 
            // Construct a Pager shape
            var count = projection.Count();
            var pagerShape = Shape.Pager(pager).TotalItemCount(count);
 
            // Create the viewmodel
            var model = new CorporateEmailsIndexVM(corporateEmails.ToList(), search, pagerShape);
 
            return View(model);
        }

...then it all works.  Not the most efficient code in the world (casting to list BEFORE filtering), but then the data is never going to be huge, so it will do.
Many thanks