Hi,
I've posted this
question on the msdn forums, where I got redirected here. I'll resume it here.
Post 1:I've got an exception with the message 'System.InvalidOperationException: Code supposed to be unreachable' originating from System.Core when performing underneath code
Code:
public ushort RemoveProvider(ulong providerId)
{
//make sure the provider isn't referenced by any device
var refDevices = (from device in DataContext.Linq<Device>()
where device.Active && device.Providers.Select(x => x.ID).Contains(providerId)
select device).ToList();
....
}
DataContext.Linq<Device>() returns a list of devices by using a NHibernate data context which is working. Each device contains 0-n providers.
refDevices should contain a list of devices where the device contains tzhe provider with the id providerId or be empty if the providerId isn't referenced anymore.
Here's the full stack trace
Code:
at System.Linq.Expressions.Compiler.StackSpiller.RewriteExpression(Expression node, Stack stack)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.Add(Expression node)
at System.Linq.Expressions.Compiler.StackSpiller.ChildRewriter.AddArguments(IArgumentProvider expressions)
at System.Linq.Expressions.Compiler.StackSpiller.RewriteMethodCallExpression(Expression expr, Stack stack)
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.VisitAndAlsoExpression(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.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.Query`1.GetEnumerator()
at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Telecontrol.SIPPR.Server.Business.Services.ConfigurationService.RemoveProvider(UInt64 providerId) in D:\dev\telecontrol\Telecontrol.SIPPR.Server\Telecontrol.SIPPR.Server.Business.Services\ConfigurationService.cs:line 477
at Telecontrol.SIPPR.Server.Communication.ServiceImplementation.ProdSw.ConfigurationWS.<>c__DisplayClassa.<RemoveProvider>b__9() in D:\dev\telecontrol\Telecontrol.SIPPR.Server\Telecontrol.SIPPR.Server.Communication.ServiceImplementation.ProdSw\ConfigurationWS.cs:line 47
at Telecontrol.SIPPR.Server.Communication.ServiceImplementation.Notification.WCFServiceBase.<>c__DisplayClassa`1.<Execute>b__8() in D:\dev\telecontrol\Telecontrol.SIPPR.Server\Telecontrol.SIPPR.Server.Communication.ServiceImplementation.Notification\WCFServiceBase.cs:line 260
at Telecontrol.SIPPR.Server.Communication.ServiceImplementation.Notification.WCFServiceBase.ExecuteInExceptionScope(Action action) in D:\dev\telecontrol\Telecontrol.SIPPR.Server\Telecontrol.SIPPR.Server.Communication.ServiceImplementation.Notification\WCFServiceBase.cs:line 125
Post 2:If executing underbeath code, everything works fine (notice the toList())
Code:
var refDevices = (from device in DataContext.Linq<Device>().ToList()
where device.Active && device.Providers.Select(x => x.ID).Contains(providerId)
select device).ToList();
This means, the sql query can't be generated by linq. If performing the filtering (where...) on the list already retrieved from the db, it works. Unfortunately, we can't use this because all the existing devices have to be retrieved from the db first.
I've got a working solution (for now) using hql:
Code:
string sqlQuery = "select COUNT(Device_id)" +
" from providerstodevices" +
" left join device on providerstodevices.Device_id = device.ID" +
string.Format(" where providerstodevices.Provider_id = {0} AND device.Active = 1", providerId);
var session = DataContext.NHibernateSession;
var queryPurge = session.CreateSQLQuery(sqlQuery);
long nbLinks = (long)queryPurge.UniqueResult();
I'd expect getting another error then 'System.InvalidOperationException: Code supposed to be unreachable' though ;)
As the message source for the exception was System.Core, at first I didn't think it was a NHibernate problem. Anyone can clarify the situation for me?