Wednesday, June 10, 2009

I am one of the moderators of the MSDN WCF Forum. One of the main areas of questions on the forum is duplex messaging – particularly using the WSDualHttpBinding. So instead of typing long messages repeating the same thing in answer to these questions I’ve decided to write this blog post to give a bit of background about duplex messaging and then discuss the options for bindings and common problems people have.

What is Duplex Messaging?

There are many ways that messages can be exchanged between two parties in a service based system: the client can send messages to the server and never get any back; the client can send a message and wait for a response; the client and service can send eachother messages without any pre-defined pattern; the client can send the service a message but not wait synchronously for a response and then then service can send a message back asynchronously; and there are many others. However, the first three of these are supported natively in WCF and are known as One-way, request/response and duplex.

So Duplex messaging is where, unsolicited, the client and service can send eachother messages. Most commonly this is characterized by the service sending the client “events” or notifications or progress of “interesting things”.

Duplex Contracts in WCF

To send messages to eachother the client and service must have an idea of what operations are available and what messages are sent and received during the communication. In WCF this idea is modelled by the contract. Now normally a contract just determines what functionality is available at the service. However, now the service is going to be sending messages to the client that the client isn’t specifically waiting for so it needs an idea of what messages the client can deal with. So we need a contract that models both directions of the conversation.

A bi-directional contract is modelled using two interfaces bound together with a ServiceContract – like this:

[ServiceContract(CallbackContract=typeof(IPizzaProgress))]
interface IOrderPizza
{
    [OperationContract]
    void PlaceOrder(string PizzaType);
}
 
 
interface IPizzaProgress
{
    [OperationContract]
    void TimeRemaining(int minutes);
 
    [OperationContract]
    void PizzaReady();
}

The import bit here is the CallbackContract that establishes the relationship between the service’s and client’s contracts.

Writing the Service

The service is implemented normally apart from two issues: firstly it needs to access the callback contract to be able to send messages back to the client; secondly the communication infrastructure (modelled by the binding) needs to be able to cope with duplex messaging. Firstly lets look at accessing the callback contract:

class PingService : IOrderPizza
{
    IPizzaProgress callback;
 
    public void PlaceOrder(string PizzaType)
    {
        callback = OperationContext.Current.GetCallbackChannel();
 
        Action preparePizza = PreparePizza;
        preparePizza.BeginInvoke(ar => preparePizza.EndInvoke(ar), null);
    }
 
    void PreparePizza()
    {
        for (int i = 10 - 1; i >= 0; i--)
        {
            callback.TimeRemaining(i);
            Thread.Sleep(1000);
        }
        callback.PizzaReady();
    }
}

The critical line here is calling GetCallbackContract on the OperationContext. This gives the service access to a proxy to call back to the client.

Now the service also needs to use a contract that is compatible with duplex messaging. WSHttpBinding is the default for the built in WCF projects but it does not support duplex messaging. People generally then move to the WSDualHttpBinding which is similar to the WSHttpBinding but does support duplex. I will go into more depth about bindings for duplex shortly but for now lets stick to this for now - it will work in our test rig on a single machine without issue.

Writing the Client

If the client is going to receive these messages it needs to provide an implementation of the callback contract. It can gets its definition from either a shared contract assembly or from metadata. If using metadata the callback contract will be named the same as the service’s contract but with the work Callback appended. It will also need to supply this implementation to the WCF infrastructure and it does this by wrapping an instance in an InstanceContext object and passing it to the proxy constructor. So here is the client:

class Program
{
    static void Main(string[] args)
    {
        InstanceContext ctx = new InstanceContext(new Callback());
 
        OrderPizzaClient proxy = new OrderPizzaClient(ctx);
 
        proxy.PlaceOrder("Pepperoni");
 
        Console.WriteLine("press enter to exit");
        Console.ReadLine();
    }
}
 
class Callback : IOrderPizzaCallback
{
    public void TimeRemaining(int minutes)
    {
        Console.WriteLine("{0} seconds remaining", minutes);
    }
 
    public void PizzaReady()
    {
        Console.WriteLine("Pizza is ready");
    }
}

Running the service and the client will have this working quite happily – so it would seem that duplex messaging and WCF works very well … so why on earth do people keep asking questions about it on the WCF forums?

It Worked on My Machine but Broke when we Deployed It!

Ahh well you probably did the thing that is obvious but almost always a bad idea. You went and chose WSDualHttpBinding as your duplex binding. To understand why this is a bad idea we need to dig a little deeper into how the WSDualHttpBinding works. HTTP is a unidirectional protocol: the client makes a request and the server sends a response. There is no way for a server to initiate an exchange with the client. So how on earth is duplex messaging going to work because it requires exactly this facility? Well the “Dual” in the name is significant, the WSDualHttpBinding actually consists of two connections: one outbound from client to server and one inbound from server to client – this second connection may already be ringing alarm bells with you. The are a two big problems with inbound connections to a client: firewalls very often block inbound connections to clients; the client may not be reachable from the server, it may be using NAT translation behind a router and so cannot be contacted without port forwarding being set up on the router. Both of these issues are showstoppers in real network topologies. You can take some small steps to help – you can specify what port the client should listen on for example by using the clientBaseAddress property of the WSDualHttpBinding. This means the network admin will only have to punch one hole in their firewall (but lets face it, network admins don’t allow any holes to be punched in the firewall).

So if you really shouldn’t use WSDualHttpBinding for duplex, what should you use instead? Well NetTcpBinding supports duplex out of the box and the nice thing about this is that the outbound connection that it establishes can also be used be used for inbound traffic – suddenly we don;t have the inbound connection firewall/NAT issues. “But hold on, isn’t NetTcpBinding for intranet? I’ve read books that tell me that in their ‘which binding should I use?’ flowcharts!” Well it turns out those flowcharts are talking rubbish – NetTcpBinding works very happily over the internet, its just not interoperable by design. “Aha! but I need interop so WSDualHttpBinding is for me!” Well unfortunately not, NetTcpBinding is non-interoperable by design, WSDualHttpBinding is non-interoperable despite its design. From the name it would suggest interoperability but  Arun Gupta from Sun wrote this excellent post describing why it wasn’t.

So now seeing that we really are not talking about interop anyway, NetTcpBinding is far more useful than WSDualHttpBinding. Its not bullet proof, if the firewall only allows outbound port 80 but also allows inbound port 80, then WSDualHttpBinding would work where NetTcpBinding wouldn’t – but in this situation we’re really talking server to server and so I’d argue its probably better to roll your own bidirectional communication with two standard HTTP based connections.

The final option you have for duplex communication is to add a piece of infrastructure into the mix. The .NET Services Service Bus (part of the Azure platform) allows two parties to exchange messages both making outbound connections – potentially even using HTTP port 80. The two outbound connections rendezvous in the Service Bus which mediates their message exchanges. If the receiver has had to use outbound port 80 then it polls to receive message bound for it.

It Worked for the First 10 Clients and then the Rest Timed Out!

Irrespective if which of the standard bindings you are using, duplex assumes a constant relationship between proxy and service. In WCF this idea is modelled by the concept of session. All duplex bindings require session. A while back I wrote in some detail about sessions. You will have to either put up with increasing the session throttle (see the linked article for details) or roll your own custom binding that can do duplex without session – you can find an example of this here.

I Use the Callback While the Client is calling the Service and it Deadlocks!

This is because your client is probably a Rich Client GUI based application (Windows Forms or WPF). To understand why this is a problem we need to step back briefly and look at UI clients, threading and WCF threading. UI applications have a rule: you must only update the UI from the thread that created those UI components. In general a GUI application has one UI thread so anything that changes the UI needs to be done from that thread. .NET 2.0 introduced a new construct to simplify the process of a background thread updating the UI: SynchronizationContext. The idea is that a UI framework creates an implementation of a SynchronizationContext derived class that handles the mechanics of marshalling a call on to the UI thread. An instance of this implementation is then made available on the UI and accessible via the SynchronizationContext.Current.

WCF adds more complexity into the mix by enforcing a rule that says “unless you tell me otherwise I will only allow one thread at a time into an object that I control”. You see this with singleton services that will only allow one call at a time by default. The same is also true of the callback implementation object – so WCF will only allow one active thread in the client at a time. So while WCF is performing an outbound call it will not allow an inbound call into the object. This causes the initial problem with the deadlock that the service’s callback cannot be dispatched while the client’s outbound call is in progress. To solve this we use the “unless you tell me otherwise” part of the above rule. You do this by annotating the callback implementation class with a [CallbackBehavior] attribute like this:

[CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant)]
class Callback : IOrderPizzaCallback
{
    public void TimeRemaining(int minutes)
    {
        Console.WriteLine("{0} seconds remaining", minutes);
    }
 
    public void PizzaReady()
    {
        Console.WriteLine("Pizza is ready");
    }
}

But now there is another problem: by default WCF will attempt to dispatch using an available SynchronizationContext. The problem with this callback is the UI thread is already blocked in an outbound call. SO for the call to dispatch we need to tell WCF not to use the SynchronizationContext – again using the CallbackBehavior attribute:

[CallbackBehavior(ConcurrencyMode=ConcurrencyMode.Reentrant, UseSynchronizationContext=false)]
class Callback : IOrderPizzaCallback
{
    ...
}

Now the issue is of course that the call is going to be processed on a non UI thread so you would have to manually marshal any UI interaction using the SynchronizationContext.Post method.

Duplex messaging can be a useful message exchange pattern but in WCF there can be some unexpected issues. Hopefully this blog post clarifies those issues and demonstrates workarounds for them.

 |  | 
Wednesday, June 10, 2009 5:54:13 AM (GMT Daylight Time, UTC+01:00)  #    Comments [9]TrackbackTracked by:
http://topsy.com/trackback?utm_source=pingback&utm_campaign=L2&url=http://www.do... [Pingback]
Monday, June 22, 2009 8:13:44 AM (GMT Daylight Time, UTC+01:00)
Hi Richard,

Great article (and I liked your screen casts on rock solid knowledge too).

Oh and by the way there are a couple of typos in this sentence: "So while WCCF is performing at outbound call it will not allow an inbound call into the object." (ie the WCCF bit and the "at outbound call" instead of "an outbound call" :)
Monday, June 22, 2009 11:07:13 AM (GMT Daylight Time, UTC+01:00)
Hi Chris

Thanks for letting me know about the typos - now fixed

Also I'll be putting more screencasts up on rocksolidknowledge.com in the next couple of weeks
Richard
Wednesday, August 26, 2009 9:04:39 PM (GMT Daylight Time, UTC+01:00)
intresting article. ppl have to read alot of book to find this out. so you would suggest nettcp or dualhttp. how would you enable nettcp in iis 5 or 6? I know iis 7 allow nettcp! Any thoughts?
Wednesday, September 02, 2009 7:59:42 PM (GMT Daylight Time, UTC+01:00)
Hi Richard,

Really a nice post!

We have developed a client WPF application consuming a set of services hosted on IIS 6.
We have a common cache of data shared by all clients that had to be synchronzed.
Now, some consultants discouraged us on using any "dual WCF" solution in order to synchronze clients as it's not suitable for hundread or few thousands of clients, so we use a mixed solution with WCF for request/reply and pure MSMQ (services->clients) to synchonize clients.

Do you have any suggestion for this scenario?
Will WCF 4.0 have some news concerning dual binding, or improved scalability?

Thanx for your reply...

-Claudio
Tuesday, September 15, 2009 12:51:49 PM (GMT Daylight Time, UTC+01:00)
Hi Mohit

I would suggest netTcpBinding for duplex work. Unfortunately this really means self0hosting rather than using IIS
Richard
Thursday, October 08, 2009 7:45:25 AM (GMT Daylight Time, UTC+01:00)
Hi Richard,

Firstly, This is a great article, really appreciate the work put in. I have few questions working of WCF applications over internet using tcp, please forgive me if they seem repetitive to what you might have already addressed in your article above.

Background: I am building an distributed collaborative Application (a similar model to several chat applications).Where a central server app rellays (Data) info to all clients participating & subscribed at server.
I initally built this application using .NET Remoting (using tcp ). Built for client machines that can be sitting behind a private LAN/router networks, but that lead to a probelm where server app failed to resolve the client ip address on callback. This was a real show-stopper. Because this applciation was is built to be used by students sitting in their homes.

TroubleShooting: Well! I tried following this kb article on msdn [http://support.microsoft.com/default.aspx/kb/939525], but still was unable to get around it.

Current: Finally, I got introduced to WCF. Now a I am again facing a similar predicament if I do rebuild by server app in WCF, will it seamlessly be able to relay messages back-n-forth over the internet.

Forums are cluttered with half-informations about this problem, most notable MSDN WCF forum about this simple question : "Can a server application using a tcp transport binding, be able to resolve a client's ip address who is sitting behind his own private newtork (read:router)"

You address this question in your article -"...NetTcpBinding supports duplex out of the box and the nice thing about this is that the outbound connection that it establishes can also be used be used for inbound traffic – suddenly we don;t have the inbound connection firewall/NAT issues. ..."

But I am still reluctant, I just read this article about a application on CodeProject - DrawMe [http://www.codeproject.com/KB/WCF/DrawMeWCF.aspx](which is similar to my application) they have also used WCF via tcp , where they specifically mention that if you want to use the app with two more clients over the internet , you would have enable port-forwarding.

Which it seems to contradict your statements. As you mentioned WCF tcp binding should take care of it, hence should not require port-forwarding. Please correct if I am wrong in my understanding.

Can you please help make sense of all of this. I just want that server-end of my application to successfully resolve the subscribed client's ipAddress so that it can rellay info back to client instance of dataContext.

Is this achievable using WCF's -tcp bindings ??

regards
Rupen

p.s : I apologize for such a long comment, I have been stuck on this for last two months. And your article is a ray of hope.
Sunday, October 18, 2009 11:02:54 AM (GMT Daylight Time, UTC+01:00)
Hi... Richard, nice to know you.
I have a some problem about wcf chat based on client-server. How to make a deal between server and client, so in server side able to send message, show active client, kick user. If you want, i can send an error project.
Really thanks for your response to my mail. donny.ph@live.com
Friday, August 06, 2010 1:53:20 PM (GMT Daylight Time, UTC+01:00)
Rupen

the application on codeplex appears to allow any client to be the "server" and therefore it would have to be able to accept an inbound connection from the other clients. For this to work a router /firewall would have to forward on the call. For clients acting purely as clients they do not need port forwarding as they are only making outbound connections.

Categorically: You do not need special client-side inbound firewall rules for a client callback using netTcpBinding
Richard
Friday, August 06, 2010 1:54:27 PM (GMT Daylight Time, UTC+01:00)
Donny,

do you have an existing application that is not working? if so can you tell me what the error is you are seeing with an exception trace?
Richard
Name
E-mail
Home page

Comment (HTML not allowed)  

Enter the code shown (prevents robots):