Implementing client callback functionality in WCF

Posted by PoweredByOrange on Stack Overflow See other posts from Stack Overflow or by PoweredByOrange
Published on 2013-10-18T21:50:35Z Indexed on 2013/10/18 21:53 UTC
Read the original article Hit count: 220

Filed under:
|
|

The project I'm working on is a client-server application with all services written in WCF and the client in WPF. There are cases where the server needs to push information to the client. I initially though about using WCF Duplex Services, but after doing some research online, I figured a lot of people are avoiding it for many reasons.

The next thing I thought about was having the client create a host connection, so that the server could use that to make a service call to the client. The problem however, is that the application is deployed over the internet, so that approach requires configuring the firewall to allow incoming traffic and since most of the users are regular users, that might also require configuring the router to allow port forwarding, which again is a hassle for the user.

My third option is that in the client, spawns a background thread which makes a call to the GetNotifications() method on server. This method on the server side then, blocks until an actual notification is created, then the thread is notified (using an AutoResetEvent object maybe?) and the information gets sent to the client. The idea is something like this:

Client

private void InitializeListener()
{
    Task.Factory.StartNew(() =>
    {
        while (true)
        {
            var notification = server.GetNotifications();

            // Display the notification.
        }
    }, CancellationToken.None, TaskCreationOptions.LongRunning, TaskScheduler.Default);
}

Server

public NotificationObject GetNotifications()
{
    while (true)
    {
         notificationEvent.WaitOne();
         return someNotificationObject;
    }
}

private void NotificationCreated()
{
    // Inform the client of this event.
    notificationEvent.Set();
}

In this case, NotificationCreated() is a callback method called when the server needs to send information to the client.

What do you think about this approach? Is this scalable at all?

© Stack Overflow or respective owner

Related posts about c#

Related posts about wcf