3

I've written a WCF webservice that takes XML files and stores them into the database. Everything worked fine under 'low load' but under high load I'm getting some unexpected behavior and thusfar I haven't been able to pinpoint what exactly the problem is. Does anybody have a suggestion?

This is the exception I'm seeing in the logs 'sometimes' - like 25 times out of 10 000:

Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.Objects.ObjectStateManager.DetectConflicts(IList`1 entries)
   at System.Data.Objects.ObjectStateManager.DetectChanges()
   at System.Data.Entity.Internal.InternalContext.GetStateEntry(Object entity)
   at System.Data.Entity.DbContext.Entry(Object entity)
... rest of my stacktrace

I see this happen every once-in-a-while and I'm currently looking into whether this has to do with concurrency (some other thread maybe working on the same entity). Can someone maybe give me some pointers as to where to look for?

Jochen van Wylick
  • 5,303
  • 4
  • 42
  • 64
  • It's either a bug in EF or you're running an old version of it. Source is available here: http://entityframework.codeplex.com/SourceControl/latest#src/EntityFramework/Core/Objects/ObjectStateManager.cs you can check you have the same with a tool such as .NET Reflector or ILSpy. – Simon Mourier Aug 26 '13 at 16:54
  • I'm running NuGet package EF 5 on this app, but I think it falls back to 4.1.1. since it's .NET 40. So basically I should upgrade to .NET 4.5 and EF 5 or 6. Hmmm... OK - I'll check with OPS. – Jochen van Wylick Aug 27 '13 at 07:30

1 Answers1

0

NullReferenceException Occurs when You try to use a reference variable who's value is Nothing/null.

When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.

I can't tell what the problem is, But i believe its with the threads. Since its working fine for small number of users. It might have used multiple threads for improving performance when the load increased. When threads executes asynchronously, there is a greater chance for having this issue.!!

The Solution i can offer is to custom specify the threads, and synchronise the Objects. Probably it will solve the Issue.

Dileep
  • 5,362
  • 3
  • 22
  • 38
  • Thanks - for your addition. Indeed - there are multiple threads running. More than 10 at a time. I'm using http://unitywcf.codeplex.com/ with the HierarchicalLifetimeManager to manage the lifetime of my datacontexts. Contexts are created per thread, per request and disposed after. In case of loads less than 10 per second, nothing happens, only when the load peaks at higher values. So my guess is that in the normal execution path, I'm not the one responsible for the nullreference exception. – Jochen van Wylick Sep 11 '13 at 07:36
  • @spike Probably the one who manages the thread is responsible. But its you who must point out what part that must be threaded. – Dileep Sep 11 '13 at 08:17