What caused this InvalidOperationException using LINQ to SQL?

We experienced a number of errors in our online application a week or two ago that have so far avoided explanation. We saw these errors internally, and they were also tested by customers, as it manifested itself in a set of web services.

I have included the internal exception below, the project uses the CSLA infrastructure and an error occurred while retrieving the object from the database.

At the time when we began to experience errors, no known changes were made to the system, the infrastructure consists of several balancing web servers.

Errors seemed to be isolated from one of our servers, we experienced them using a console application that connects to web services. The server in question used the local DMZ IP address to resolve web services in its hosts file and, forcing it to go from the outside, it seemed to resolve the problems.

There seems to be a very fine line between the application and the infrastructure to isolate this, so I wonder if anyone has any ideas or theories that could explain this ?

<InnerException> <ExceptionType>System.InvalidOperationException, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</ExceptionType> <Message>Exception of type 'System.InvalidOperationException' was thrown.</Message> <Source>mscorlib</Source> <HelpLink /> <Property name="Data">System.Collections.ListDictionaryInternal</Property> <Property name="TargetSite">Void VerifyIntegrity()</Property> <StackTrace> at System.Runtime.CompilerServices.ConditionalWeakTable`2.VerifyIntegrity() at System.Runtime.CompilerServices.ConditionalWeakTable`2.Add(TKey key, TValue value) at System.Linq.Expressions.Expression..ctor(ExpressionType nodeType, Type type) at System.Data.Linq.SqlClient.Translator.TranslateLink(SqlLink link, List`1 keyExpressions, Boolean asExpression) at System.Data.Linq.SqlClient.SqlBinder.Visitor.ConvertToFetchedExpression(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.ConvertLinks(SqlExpression node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.FetchExpression(SqlExpression expr) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitMember(SqlMember m) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitNew(SqlNew sox) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitExpression(SqlExpression expr) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitAlias(SqlAlias a) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlVisitor.VisitSource(SqlSource source) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitSelect(SqlSelect select) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Visitor.VisitIncludeScope(SqlIncludeScope scope) at System.Data.Linq.SqlClient.SqlVisitor.Visit(SqlNode node) at System.Data.Linq.SqlClient.SqlBinder.Bind(SqlNode node) at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(ResultShape resultShape, Type resultType, SqlNode node, ReadOnlyCollection`1 parentParameters, SqlNodeAnnotations annotations) at System.Data.Linq.SqlClient.SqlProvider.BuildQuery(Expression query, SqlNodeAnnotations annotations) 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.SingleOrDefault[TSource](IQueryable`1 source) at NamespaceA.DaSql.NamespaceB.NamespaceBContext.NamespaceA.Da.NamespaceB.INamespaceBContext.GetClassA(Int32 objectId) at NamespaceA.NamespaceB.ClassA.DataPortal_Fetch(SingleCriteria`2 criteria) at dm(Object , Object[] ) at Csla.Reflection.MethodCaller.CallMethod(Object obj, DynamicMethodHandle methodHandle, Object[] parameters)</StackTrace> </InnerException> 

Thanks in advance for any help or theory.

Edit:

Full exception here

LINQ to SQL is below, nothing for him and ObjectA is just a wrapper class with properties. Nothing but a simple selection and filling of one object is based on ID. ctx is a CSLA ContextManager.

 var data = from d in ctx.DataContext.ObjectAs where d.ObjectId == objectId select new ObjectA { Id = d.DispatchId, ClientId = d.ClientId, DateCreated = d.DateCreated }; return data.SingleOrDefault(); 
+6
source share
3 answers

I see the internal exception that you have connected, and according to the exception, there may be a problem with your collection, which is created when the SingleOrDefault () method is run (LINQ is executed with a defer).

I am posting 2 links that can help you better analyze the problem and solution if you delve a little into creating the collection. Hope these links help you solve your problem:

Link 1: http://typedescriptor.net/browse/members/289638-System.Runtime.CompilerServices.ConditionalWeakTable%602%5BTKey,TValue%5D.VerifyIntegrity ()

Link 2: http : //code.google.com

The methods of the criminal can be ConditionalWeakTable.Add () and ConditionalWeakTable.VerifyIntegrity ()

The above pointers should help you reach a solution. Thanks

+2
source

I think that, as shown in the msdn documentation , an InvalidOperationException can be InvalidOperationException if the SingleOrDefault() method is called in a sequence that contains more than one element, maybe you could use FirstOrDefault() instead

Hope this helps.

0
source

I would set a breakpoint on

 return data.SingleOrDefault(); 

lines. Then, hover over the data to evaluate it, and see if it returns what you expect. This may indicate that an exception has been thrown. If an exception is not thrown, try evaluating data.SingleOrDefault () either with the mouse or by adding the watch expression, and see if you get an exception.

You can also split the query into one part for each step and evaluate at each step to determine which call throws an exception.

Example:

 // break execution and evaluate each one separately var table = ctx.DataContext.ObjectAs; var filter = table.Where(x => x.ObjectId == objectId); var select1 = filter.Select(x => x.DispatchId); var select2 = filter.Select(x => x.ClientId); var select3 = filter.Select(x => x.DateCreated); var select4 = filter.Select(x => new ObjectA { Id = d.DispatchId, ClientId = d.ClientId, DateCreated = d.DateCreated }; var singleOrDefault = select4.SingleOrDefault(); 

This narrows how much of your request is causing the problem.

0
source

Source: https://habr.com/ru/post/895607/


All Articles