Tags: , , , , | Categories: .NET Posted by oleksii on 9/24/2011 2:41 PM | Comments (0)

I was trying to create a simple project based on WCF where a service can notify clients about something (as oposed to the standard scenarios where a client asks a service to perform some operation). The simplest way to do so is to use callbacks. I have made up a very simple solution to demonstrate this concept. A sample solution has two interfaces: one for the usual service contract and another one for the client callback.

[ServiceContract(CallbackContract = typeof(IContractCallback))]
public interface IContract
{
    [OperationContract]
    void Foo();
}

[ServiceContract]
public interface IContractCallback
{
    [OperationContract]
    void OnFooCallback();
}

A very basic implementation of these contract can look like this

internal class WcfService : IContract
{
    public void Foo()
    {
        //Do work...            
        var callback = OperationContext.Current
                        .GetCallbackChannel<IContractCallback>();
        callback.OnFooCallback();
    }
}

internal class ContractCallback : IContractCallback
{
    public void OnFooCallback()
    {
        Console.WriteLine("...");
    }
}

This is how this small app looks

Using the callback contract can be straight-forward, however there are some limitations:

  • Only NetTcpBinding, NetNamedPipeBinding and WSDualHttpBinding bindings are supported
  • One callback contract per service contract is allowed
  • Client must keep the connection open the whole time
  • Service must use reentrant or multiple concurrency mode

This project can be downloaded using the link below or from the github.

CallbackService.zip (17.49 kb) [Downloads: 121]

If you enjoyed this post, make sure you subscribe to my RSS feed!

blog comments powered by Disqus