EF Compiled Query occasionally causing SqlException
I'm using a compiled query which is quite complicated to perform a portion of my querying logic in an application, which abstracts the compiled query behind a Func<>. I'm periodically (once in a thousand or so runs) receiving the following Exceptions:
SqlException:
The parameterized query '(@p__linq__0 int,@p__linq__1 nvarchar(4000),@p__linq__2 varchar(' expects the parameter '@p__linq__1', which was not supplied.开发者_运维技巧
And InvalidOperationException:
Parameters cannot be added or removed from the parameter collection, and the parameter collection cannot be cleared after a query has been evaluated or its trace string has been retrieved.
My first reaction, after seeing the second exception, is that I'm bumping into concurrency issues. This is done from a WCF service using basicHttpBinding at default configuration for service behavior, this means concurrency mode is single. I'm not using InstanceContextMode single, but instead the default PerSession, would this potentially cause an issue?
The database involved is heavily used, so I've also suspected that perhaps I'm timing out during a query, but I can't draw a logical correlation to the exceptions I'm seeing.
To add to the problem, I'm not able to reproduce these errors on local machines, the exceptions are occuring during a processing routine on remote servers during the evenings, and only once or twice a night. Hopefully somebody else has experienced something similar. I can try a few things, such as changing the instancecontextmode, adding an increased SQL Server timeout, but I'd prefer to know what the problem is before attempting work-arounds. I did record a few instances of the exception, here's the stack frame for one of the InvalidOperationExceptions:
System.Data.Objects.ObjectParameterCollection.Add(ObjectParameter parameter)
System.Data.Objects.ELinq.CompiledELinqQueryState.GetExecutionPlan(Nullable`1 forMergeOption)
System.Data.Objects.ObjectQuery`1.GetResults(Nullable`1 forMergeOption)
System.Data.Objects.ObjectQuery`1.System.Collections.Generic.IEnumerable<T>.GetEnumerator()
System.Linq.Enumerable.Count[TSource](IEnumerable`1 source, Func`2 predicate)
Processor.Processor.<>c__DisplayClass10.<GetObjectContexts>b__9(ObjectContextMetadata q) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 174
System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
System.Linq.Enumerable.<DistinctIterator>d__81`1.MoveNext()
System.Linq.Enumerable.Contains[TSource](IEnumerable`1 source, TSource value, IEqualityComparer`1 comparer)
Processor.Processor.<>c__DisplayClass10.<GetObjectContexts>b__c(ObjectContextMetadata q) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 179
System.Linq.Enumerable.WhereEnumerableIterator`1.MoveNext()
Processor.Processor.ResolveGeoCode(AddressItem address, String product) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\Processor\Processor.cs:line 273
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepotWithMask(AddressItem address, String product, DeliveryMap map, IGeocoder geocoder) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 136
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepotWithMask(AddressItem address, String product, DeliveryMap map) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 59
Company.ProcessingLogic.SomeService.SomeServiceService.ResolveDepot(AddressItem address, String product) in C:\Documents and Settings\eugarps\My Documents\SomeService-development\SomeServiceService\SomeServiceService.cs:line 53
SyncInvokeResolveDepot(Object , Object[] , Object[] )
System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs)
System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc& rpc)
System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc& rpc)
System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean isOperationContextSet)
It could be related to the data you are sending in. Check if you are sending in null.
Edit
It could also be a race condition. Is it possible for 2 threads to be building a query at the same time, using the same object. If so you could get a situation where the same parameter is added twice.
精彩评论