1

I have a C# program which will have multiple instances that need to communicate with each other, executing commands and sending data back and forth. Right now, this is accomplished using WM_COPYDATA, which is quite cumbersome. I would like to upgrade this to a WCF system using NetNamedPipeBindings. However, no matter what settings I try, I cannot get reentrant duplex communication working.

This would be a normal WCF call:

client calls proxy.foo
   server executes foo
   server finishes foo
client returns from proxy.foo

And this is what I want:

client calls proxy.foo
    server executes foo
    server calls callback.bar
        client executes bar
        client calls proxy.baz
            server executes baz
            server finishes baz
        client returns from proxy.baz
        client finishes bar
    server returns from callback.bar
    server finishes foo
client returns from proxy.foo

Critically, I want the client thread that calls proxy.foo to be the SAME THREAD that calls proxy.baz. For Windows Messages, this is par for the course. But no matter what settings I try, I just can't get this pattern working through WCF. Is it even possible?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Paul Accisano
  • 1,416
  • 1
  • 14
  • 25

1 Answers1

2

Unless you make the calls async and manually handle the interleaving and (probably) marshaling to the right thread, I can not see how it is possible. After all, the client (thread) is calling the server and is blocked until the server returns with a response. So the call back to client will deadlock the code, as the client thread is already blocked.

Reonekot
  • 402
  • 4
  • 10
  • Sigh... I guess I'm sticking with COPYDATA then. I'm very surprised that WCF doesn't support this. – Paul Accisano Jan 29 '12 at 19:21
  • 1
    I did some testing on this and while the above is somewhat correct, it is not the full story. :S If you set ConcurrencyMode = ConcurrencyMode.Reentrant and the clint thread has a syncronization context (like in a WinForm or create one yourself) the callback will be marchalled automatically to the original thread. It also requires the server calls to implement the async pattern, since (as written above) the original call from the client need to be made async'ed. I made a "small" test project you can check out: [link](http://www.noer.it/upload/Test_WcfReentrant.zip) – Reonekot Feb 01 '12 at 09:00