使用单实例显示一个客户端所做的任何更改,以供 WCF C# 中的其他客户端看到

Show any changes made by one client to be seen by other clients in WCF C# using Single instance

这是我正在处理的项目中的一段代码,它让多个客户端连接到 WCF 服务(主机),主机将检测对服务库数据库所做的任何更改并显示给每个客户端所做的更改,它显示更改的值,进行更改的客户端的 IP 地址。

    private void RaisedPropertyChanged(string prop)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, new PropertyChangedEventArgs(prop));
    }
    public string GetIP()
    {
        OperationContext context = OperationContext.Current;
        MessageProperties prop = context.IncomingMessageProperties;
        RemoteEndpointMessageProperty endpoint =
           prop[RemoteEndpointMessageProperty.Name] as RemoteEndpointMessageProperty;
        string ip = endpoint.Address;
        Console.WriteLine("Client Ip" + ip);

        return ip;
    }
  • 一种方法是使用双工合同

How to: Create a Duplex Contract

The duplex contract is one of three message patterns available to Windows Communication Foundation (WCF) services. The other two message patterns are one-way and request-reply. A duplex contract consists of two one-way contracts between the client and the server and does not require that the method calls be correlated. Use this kind of contract when your service must query the client for more information or explicitly raise events on the client

  • 你也可以使用Signalr

Introduction to SignalR

ASP.NET SignalR is a library for ASP.NET developers that simplifies the process of adding real-time web functionality to applications. Real-time web functionality is the ability to have server code push content to connected clients instantly as it becomes available, rather than having the server wait for a client to request new data.

  • 最后,您可以轮询 Wcf 服务以获取上次更改信息

WFCDuplex Contracts 是一种非常安全可靠的实现结果的方式。 Signalr 是一种非常轻量级的方法,并且不够健壮或安全。

在这种情况下,如果您不需要安全性和轻量级方法,我倾向于使用 Signalr