EF Compiled Query偶尔会导致SqlException

时间:2010-11-18 19:49:26

标签: c# wcf entity-framework thread-safety

我正在使用编译的查询,在应用程序中执行我的查询逻辑的一部分非常复杂,它将编译的查询抽象为Func<>。我定期(大约一千次左右)收到以下例外:

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.  

在看到第二个例外后,我的第一反应是我遇到了并发问题。这是在WCF服务中使用basicHttpBinding在默认配置中为服务行为完成的,这意味着并发模式是单一的。我没有使用InstanceContextMode单一,而是默认的PerSession,这可能会导致问题吗?

所涉及的数据库使用频繁,所以我也怀疑在查询过程中我可能会超时,但我无法与我看到的异常进行逻辑关联。

要添加问题,我无法在本地计算机上重现这些错误,在晚上的远程服务器上的处理例程期间会发生异常,并且每晚只发生一次或两次。希望其他人经历过类似的事情。我可以尝试一些事情,例如更改instancecontextmode,添加增加的SQL Server超时,但我更愿意在尝试解决之前知道问题所在。我确实记录了一些异常实例,这里是其中一个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)

1 个答案:

答案 0 :(得分:0)

它可能与您发送的数据有关。请检查您是否发送了空。

修改

这也可能是竞争条件。 2个线程是否可以使用相同的对象同时构建查询。如果是这样,你可能会遇到两次添加相同参数的情况。