dngchn
dngchn

Reputation: 19

How can I send a data from WCF host to connected client?

I want to send a data from WCF host (not service proxy) to the connected client with the service. How can I achieve this?

Upvotes: 0

Views: 3999

Answers (1)

Jacob
Jacob

Reputation: 78840

You'll need to create a Duplex service. See this article for more information: http://msdn.microsoft.com/en-us/library/ms731064.aspx

Here's an example:

[ServiceContract(
    SessionMode=SessionMode.Required,
    CallbackContract=typeof(INotificationServiceCallback))]
public interface INotificationService
{
    [OperationContract(IsOneWay = true)]
    void Connect();
}

public interface INotificationServiceCallback
{
    [OperationContract(IsOneWay = true)]
    void SendNotification(string notification);
}

public class NotificationService : INotificationService
{
    public static List<INotificationServiceCallback> Clients = 
        new List<INotificationServiceCallback>();

    public void Connect()
    {
        Clients.Add(
            OperationContext.Current.GetCallbackChannel<ICalculatorDuplexCallback>());
    }
}

public class Notifier
{
    void HandleReceivedNotification(string notification)
    {
        foreach (var client in NotificationService.Clients)
        {
            client.SendNotification(notification);
        }
    }
}

Upvotes: 2

Related Questions