>
 Thursday, August 16, 2007
« System Setup for .NET 3.0 and .NET 3.5 | Main | A comical story about cell phone depende... »

Today I completed a webcast as part of a 15 part series - today's subject concurrency, throughput and throttling. I received some questions about callback and Windows client applications that I thought I would elaborate on here. In fact, I went a little overboard and created a bunch of samples that would illustrate the behavior of services and clients when you have a Windows client, a service with a callback contract (thus, two-way communication) and various WCF settings at the client and service that relate to concurrency, multithreading, synchronization with the UI thread, and so on.

The following table summarizes various settings at the client, service and callback and the resulting behavior at runtime. Here's the breakdown for each column:

  • Callback Sync Context - refers to the UseSynchronizationContext setting for the CallbackBehaviorAttribute on the client callback object.
  • Callback Concurrency Mode - refers to the ConcurrencyMode setting for the CallbackBehaviorAttribute on the client callback object.
  • Service Operation - Indicates if the service contract operations are one-way or two-way.
  • Callback Operation - Indicates if the callback contract operations are one-way or two-way.
  • Service Concurrency Mode - refers to the ConcurrencyMode setting for the ServiceBehaviorAttribute on the service type.
  • Resulting Behavior - when the corresponding sample is run, what happens?

I have also uploaded sample code for each of these scenarios, numbered in order of table row description below. Get the code here.

Callback Sync

Context

Callback Concurrency Mode

Service Operation

Callback Operation

Service Concurrency Mode

Resulting Behavior

True/

False

Single/

Reentrant/

Multiple

One-way/

Two-way

Two-way

Single

These are defaults. InvalidOperationException at the service. Operation would deadlock calling the client application since the callback is two-way and reentrancy isn’t possible.

True

Single/

Reentrant/

Multiple

Two-way

Two-way

Reentrant/

Multiple

Service able to call client, but client is blocked because of outgoing call.

False

Single/

Reentrant/

Multiple

Two-way

Two-way

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. If client callback object tries to communicate with UI thread (i.e., setting properties of controls) it will block.

False

Single

One-way

Two-way

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. Client callback object can communicate with UI thread using Invoke() from callback thread. Client can issue multiple calls to service but only one callback can be processed at a time.

False

Multiple

One-way

Two-way

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. Client callback object can communicate with UI thread using Invoke() from callback thread. Client can issue multiple calls and multiple callbacks can be processed at a time.

False

Multiple

One-way

One-way

Single/

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. Client callback object can communicate with UI thread using Invoke() from callback thread. Client can issue multiple calls and multiple callbacks can be processed at a time.

False

Multiple

Two-way/

Multi-threaded Client

 

Two-way

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. Client callback object can communicate with UI thread using Invoke() from callback thread. Multiple calls from client can be processed at the service, and multiple callbacks can be processed at the client.

False

Multiple

Two-way/

Multi-threaded Client

Two-way

Reentrant/

Multiple

Service able to call client, client callback object is not blocked. Client callback can communicate with UI thread using Invoke() from callback thread. Multiple calls from client can be processed at the service, and multiple callbacks can be processed at the client.

Here is a summary of considerations.

  • Contract design:
    • Make callback operations one-way. If this isn’t possible, make the service reentrant. Prefer one-way callbacks since most likely the service doesn’t care about responses.
    • If service operations will notify clients with a callback, make them one-way as well.
  • Exception handling:
    • Remember that uncaught exceptions invalidate service, client and callback channels where sessions are involved. Two-way communication can only happen over duplex, thus there is a transport session (TCP, named pipes) or HTTP session (using reliable sessions over WSDualHttpBinding). 
    • Throw faults for known exceptions unless you want the client to be forced to recreate the proxy and start a new session. That includes clients – who should not allow exceptions to flow back to the service during a callback unless they intend to destroy the session.
    • To avoid destroying the session: if callback operations are one-way, swallow the exception after handling it at the client; if callback operations are two-way, report a fault to the service in response.
  • Windows client issues:
    • Always disable synchronization with the UI thread for callback objects, by setting UseSynchronizationContext to false in the CallbackBehaviorAttribute. 
    • Multithreaded clients should invoke services on another thread (can use asynchronous proxies) when callbacks or long running service operations are involved. This prevents UI thread from blocking during call, allows callbacks to communicate freely with UI.
    • Use Invoke() to communicate with UI from the callback thread. 
    • Don’t forget to protect other client state if the callback object supports multiple concurrent callbacks. That means the ConcurrencyMode setting is Multiple in the CallbackBehaviorAttribute.
Technorati Tags: , ,
8/16/2007 1:20 AM .NET 3.0 | WCF  | Comments [5]  |  View reactions  |  Trackback
    ON THIS PAGE
    SEARCH
    CATEGORIES
    ARCHIVES
    BLOGROLL

Designed by NUKEATION STUDIOS