Reputation: 6805
I have a WCF service configured like this:
InstanceContextMode = InstanceContextMode.PerCall,
ConcurrencyMode = ConcurrencyMode.Multiple
I have a method where I call the service about ten times per second for about 30 seconds. Randomly, the service stop to work and the client is stopped on the line where the service is called.
On the server side, I have a static object "MyStaticObject" (Not declared in my service, this is an other class on the server). This object is a Dictionnary that contains some instanced objects in which there is a backgroundworker.
I don't think there is a deadlock in the database since it lock when I try to access the service and not when I try to access the database.
I currently lock my dictionnary this way:
lock (MyClass.MyStaticLockObject)
{
MyClass.MyStaticObject...;
}
I'd like to know what could cause this kind of weird behavior.
Upvotes: 1
Views: 1050
Reputation: 24027
I've seen WCF services lock-up under high loads if you're creating a new proxy object on the client-end for each operation. Are you sure you're not creating 10 proxy instances per second?
If you're accessing a static instance of a Dictionary, you may want to consider locking it when you do read/write operations, eg:
lock (MyStaticObject)
MyStaticObject.Add("Foo", foo);
lock (MyStaticObject)
foo = MyStaticObject["Foo"];
Alternatively, you could try using a thread-safe implementation of Dictionary, such as the one in this article, which uses ReaderWriterLockSlim to maintain separate locks for reading and writing.
BTW: for those incredulous that a service would be called 10 times per second, I currently work on a project where it performs quite nicely on 100 calls per second--and will have to where it gets deployed.
Upvotes: 2