WCF Simplified Part 3: Using the WCF Duplex MEP

Now that we are up and running with WCF, lets try another simple example. We have already seen how to use the request/reply pattern in the introductory post. In this post we’ll how to use the Duplex (one-way) pattern. This will be an extension to the two Visual Studio 2010 solutions we built in the previous introductory post.

1. Open the WCF host application (remember to run Visual Studio 2010 as admin). Add the following code to the IService1.cs file,

// Add the callback interface
public interface ICallback {
    [OperationContract(IsOneWay = true)]
    void MyCallbackFunction(string callbackValue);

// Add the SessionMode and CallbackContract
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(ICallback))]
public interface IService1 {
    string GetData(int value);

    CompositeType GetDataUsingDataContract(CompositeType composite);

// Use a data contract as illustrated in the sample below to add composite types to service operations
public class CompositeType {
    bool boolValue = true;
    string stringValue = "Hello ";

    public bool BoolValue {
        get { return boolValue; }
        set { boolValue = value; }

    public string StringValue {
        get { return stringValue; }
        set { stringValue = value; }

2. In Service1.cs change the GetData method to call a client method,

public string GetData(int value) {
    // Get the client callback method...
    ICallback callBack = OperationContext.Current.GetCallbackChannel<ICallback>();

    // Call the client method from the server!
    return string.Format("You entered: {0}", value);


3. And in the Program.cs change the Main method to use WsDualHttpBinding instead of BasicHttpBinding,

using (ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.Service1), new Uri("http://localhost:8000/HelloWCF"))) {
    // Set up a service endpoint [Contract, Binding, Address]
    host.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1), new WSDualHttpBinding(), "HelloWCF");

    //Enable metadata exchange
    ServiceMetadataBehavior smb = new ServiceMetadataBehavior() { HttpGetEnabled = true };




4. We are all set on the server side, for the client open the client console application we built in the previous introductory post. Delete the Service Reference and re-add the Service Reference to the WCF host application (run the host first). Then change the code in Program.cs to,

public class Callbacks : ServiceReference1.IService1Callback {
    void ServiceReference1.IService1Callback.MyCallbackFunction(string callbackValue) {
        Console.WriteLine("Server called this client method: {0}", callbackValue);

class Program {
    static void Main(string[] args) {
        // Create an endpoint
        EndpointAddress epoint = new EndpointAddress("http://localhost:8000/HelloWCF/HelloWCF");
        DuplexChannelFactory<ServiceReference1.IService1> dualFactory 
                          = new DuplexChannelFactory<ServiceReference1.IService1>(new Callbacks(), new WSDualHttpBinding(), epoint);

        ServiceReference1.IService1 proxy = dualFactory.CreateChannel();
        using (proxy as IDisposable)
            // Call the WCF service using the proxy
            string s = proxy.GetData(42);

5. That’s it! We are all set now, running the client app, you should see,



About soumya chattopadhyay
I live and work in Seattle, WA. I work with Microsoft technologies, and I'm especially interested in C#.

5 Responses to WCF Simplified Part 3: Using the WCF Duplex MEP

  1. Pingback: WCF Simplified Series « I.Net

  2. Fadel Galal says:

    Excellent post, thanks.

  3. Ryan says:

    Amazing! Helped me get my program up and running! Huge success!

  4. Riaan says:

    I keep on having the following error here

    System.InvalidOperationException: Contract requires Duplex, but Binding ‘WSHttpBinding’ doesn’t support it or isn’t configured properly to support it.
    at System.ServiceModel.Description.DispatcherBuilder.BuildChannelListener(StuffPerListenUriInfo stuff, ServiceHostBase serviceHost, Uri listenUri, ListenUriMode listenUriMode, Boolean supportContextSession, IChannelListener& result)
    at System.ServiceModel.Description.DispatcherBuilder.InitializeServiceHost(ServiceDescription description, ServiceHostBase serviceHost)
    at System.ServiceModel.ServiceHostBase.InitializeRuntime()
    at System.ServiceModel.ServiceHostBase.OnOpen(TimeSpan timeout)
    at System.ServiceModel.Channels.CommunicationObject.Open(TimeSpan timeout)
    at Microsoft.Tools.SvcHost.ServiceHostHelper.OpenService(ServiceInfo info)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: