0

我想通过服务将数据从 WCF 主机(不是服务代理)发送到连接的客户端。我怎样才能做到这一点?

4

1 回答 1

3

您需要创建一个Duplex服务。有关详细信息,请参阅本文:http: //msdn.microsoft.com/en-us/library/ms731064.aspx

这是一个例子:

[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);
        }
    }
}
于 2011-02-25T04:48:31.720 回答