views:

750

answers:

3

I am trying to learn WCF. I have a simple client and server application setup and upon pressing a button on the client, it gets an updated value from the server.

My next step is I am trying to do a callback from the server to the client to update its value. I have poured through many examples, and they just seem too big and confusing. Is there anyone that can give my just the simplest example of its implementation in C#?

I keep looking through examples online and I just do not understand what it takes? Of course I could copy the example line by line but that does me no good because I still don't what to implement if I wanted to do this in my own code.

Could someone please help me with a very simple example on what steps I would need to take and what I would need to do in the server code and then in the client code to make this happen?

Thank you

+1  A: 

If I'm reading your question right, you want to have a two-way conversation between the client and the server (the server can communicate back to the client). The WSDualHttpBinding gives you this functionality.

The unfortunate reality with WCF is that there is no such thing as a simple example. It requires you to define contracts, configure the services, and use a host, and create client code. Take a look at this article for a somewhat simple example.

Michael Meadows
+9  A: 

Here is about the simplest complete example that I can come up with:

public interface IMyContractCallback
{
    [OperationContract]
    void OnCallback();
}

[ServiceContract(CallbackContract = typeof(IMyContractCallback))]
public interface IMyContract
{
    [OperationContract]
    void DoSomething();
}

[ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Reentrant)]
public class MyService : IMyContract
{
    public void DoSomething()
    {
        Console.WriteLine("Hi from server!");
        var callback = OperationContext.Current.GetCallbackChannel<IMyContractCallback>();
        callback.OnCallback();
    }
}

public class MyContractClient : DuplexClientBase<IMyContract>
{
    public MyContractClient(object callbackInstance, Binding binding, EndpointAddress remoteAddress)
        : base(callbackInstance, binding, remoteAddress) { }
}

public class MyCallbackClient : IMyContractCallback
{
    public void OnCallback()
    {
        Console.WriteLine("Hi from client!");
    }
}

class Program
{
    static void Main(string[] args)
    {
        var uri = new Uri("net.tcp://localhost");
        var binding = new NetTcpBinding();
        var host = new ServiceHost(typeof(MyService), uri);
        host.AddServiceEndpoint(typeof(IMyContract), binding, "");
        host.Open();

        var callback = new MyCallbackClient();
        var client = new MyContractClient(callback, binding, new EndpointAddress(uri));
        var proxy = client.ChannelFactory.CreateChannel();
        proxy.DoSomething();
        // Printed in console:
        //  Hi from server!
        //  Hi from client!

        client.Close();
        host.Close();
    }
}

A few namespaces will need to be included in order to run the example:

using System;
using System.ServiceModel;
using System.ServiceModel.Channels;
Ray Vernagus
Thank you so much, that example you provided is very helpful.
kevin bailey
That's awesome Ray! :) I'm bookmarking this for future reference!
Terry Donaghe
Glad it's helping! I'm a big hater of gigantic MSDN "examples" myself...
Ray Vernagus
If you'd like to consider a tool for testing WCF services containing callbacks, the beta version of WCFStorm (http://www.wcfstorm.com) supports this. Should be worth checking out especially since it's going to be released soon.
+1  A: 

Grab a copy of "Programming WCF Services, 2nd Edition" by Juval Lowy. There are large sections of the book devoted to Callback operations. In Chapter 5, start on page 214. In the chapter on Concurrency Management (ch. 8) there's even more information.

"Programming WCF Services" is more or less the WCF "bible."

Terry Donaghe