Click here to Skip to main content
15,844,673 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
So I am currently building a service and actually everything works fine... MOSTLY!
These are really worst case scenario's. I'd rather have everything fail altogether than having something that works... Mostly.

So I have been looking on the internet, reading books, studying my code thoroughly and ultimately my boss has examined the code probably even more thorouhly. And I just can't seem to fix the problem.

The problem is as follows:
I have a WCF service that I call from a client. The service does some work, does a callback to the client and then returns to the client.
Pseudo code:
class MyService : IMyService
    public void DoSomething() // OperationContract, requires some parameters in my code.
        // Does some stuff which triggers a one-way callback.
        // In this callback I update some UI stuff.
    // Other callbacks are made at an unspecified time.

// Caller.
This posed to be a problem since I cannot do a callback while the client is still waiting on the service to return. I fixed this by having Visual Studio 2010 generate me a proxy with asynchronous Methods.
Caller now looks like this:
// Caller.
service.BeginDoSomething(null, null); // No need for a Callback, just fire and forget.
// One or two callbacks are raised inside the Method, another one is raised at a later unspecified time.
Works fine, except that while my service is doing callbacks I am doing another request to the service.
String[] arr = service.GetStrings(); // Returns an array of String.
And this is exactly where things go awry. This works, sometimes. GetStrings does nothing out of the ordinary. But under certain unknown circumstances the call fails, never reaches the service and causes a timeout. Shortly after that a callback from the service to the client fails. Again sometimes. 9 out of 10 times the callback is unaffected.

Not doing callbacks solves the problem, but that's not an option. Perhaps the amount of callbacks might be a problem? I am currently calling DoSomething twenty times (for test, might be 200 in production). Every call raises three callbacks, one right after DoSomething is called, the other two at a later, unspecified time.
It would be possible to call DoSomething one time and passing it an array of (for example) 200 Objects (which would cause 600 callbacks eventually).

Could it be that there is some sort of deadlock condition where my service tries to do a callback at the same time my client does another call to GetStrings and they block each other? Calling DoSomething twenty times is no problem, as long as I don't call GetStrings (GetStrings does not return void, which might make a difference). Also I am not calling GetStrings asynchronously (shouldn't be necessary).

Again, the GetStrings call goes wrong sometimes, probably a 50/50 ratio. I'm all out of idea's. I am sure I am not the only one having this problem though :)

I do my callbacks as described in this article[^].
Updated 15-Nov-11 9:02am
Sergey Alexandrovich Kryukov 15-Nov-11 14:49pm    
Good question, voted 5.
Sander Rossel 16-Nov-11 2:57am    
Thanks SA :)
killabyte 15-Nov-11 22:11pm    
What have you configured the InstanceContextMode and ConcurrencyMode to?
Sander Rossel 16-Nov-11 2:59am    
InstanceContextMode = Single
ConcurrencyMode not set, so Single.
I guess ConcurrencyMode should be made Multiple if I follow the answer below (haven't checked it, I am going to investigate it now).
killabyte 16-Nov-11 18:34pm    
I think by the sounds of things it must be some nasty lil threading bug, i note in the call back example you cited the callbacks a Reentrant which could bugger things up if the InstanceContextMode is set to single? not sure

1 solution

Here's a great blog covering threading issues with WCF callbacks:[^]

In a nutshell, by default the callback to your client is made on the UI thread. That can end up causing various deadlock situations.

Not sure if that's your situation. Certainly when you changed the first call to asynchronous, you eliminated a number of deadlock situations with the callback being on the UI thread. But maybe there still is one.

Take a look at what threads all the call backs are happening on and what threads the service is servicing the various calls on. You might want to service the callbacks on a different thread.
Share this answer
Sander Rossel 16-Nov-11 3:57am    
Wonderful! You just solved my problem of the week/month/year :)
I was indeed having a deadlock. I used the SynchronizationContext as suggested in the blog and I can now call my GetStrings as much as I want to without problems.

From the blog:
"Unfortunately, most examples you see on WCF callbacks do only this and as soon as the developer mimics this pattern but performs some UI update to the actual form, they run into problems."

Exactly what happened to me, but couldn't get past calling the service async :)
[no name] 4-Dec-16 15:49pm    
thank you. i was writing a duplex service and all i was doing wrong was not calling the async function from client.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900