简体   繁体   English

WCF双工回调,如何向所有客户端发送消息?

[英]WCF duplex callbacks, how do I send a message to all clients?

I am using WCF with duplex netTcpBinding and I want to send a message to all users connected currently to my service. 我正在使用WCF与双工netTcpBinding ,我想向当前连接到我的服务的所有用户发送一条消息。 I thought I could just create a callback contract and it would send a message to all the clients but it seems I am mistaken, and there isn't a single server/service, each client gets its own service? 我以为我可以创建一个回调合约,它会向所有客户发送一条消息,但似乎我错了,并且没有一个服务器/服务,每个客户端都有自己的服务?

I have service with the name 'Server'. 我的服务名称为'Server'。 Here is how I access the server from the client - 以下是我从客户端访问服务器的方法 -

ServerClient client = new ServerClient();
string result = client.SendMessage(messageTextBox.Text);
client.Close();

I thought the 'Server' was a single object that handled all calls by my clients but then I've started a thread in the Server constructor and I found out that multiple threads get started because every time a client calls the Server, a new Server object is created. 我认为'Server'是一个单个对象来处理我的客户端的所有调用,但后来我在Server构造函数中启动了一个线程,我发现多个线程都启动了,因为每次客户端调用Server时,都会有一个新的服务器对象已创建。

So it seems each client has it's own service/server. 所以似乎每个客户端都有自己的服务/服务器。

  1. With that in mind how do I send a message to all my clients from the server? 考虑到这一点,我如何从服务器向我的所有客户发送消息?
  2. I thought the standard practise for accessing the server from the client was to get a proxy object, call the service functions, and then Close the proxy object like in the code above...but if I close the proxy object doesn't it mean I have closed the connection between client and server and now the server won't be able to make duplex callbacks to the client? 我认为从客户端访问服务器的标准做法是获取代理对象,调用服务函数,然后像上面的代码中那样关闭代理对象...但如果我关闭代理对象不是它的意思我已关闭客户端和服务器之间的连接,现在服务器将无法对客户端进行双工回调?

1) If you want all clients to share the same server, you need to make your service a singleton. 1)如果您希望所有客户端共享同一台服务器,您需要使您的服务成为单身人士。 Add this attribute to the class implementing your service (not the interface): 将此属性添加到实现服务的类(而不是接口):

[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)] 

That said, I suspect that what you really want is a synchronized, static (thread-shared) instance of a List<ServerClient> . 也就是说,我怀疑你真正想要的是List<ServerClient>的同步,静态(线程共享)实例。 Then you would iterate over that to send a message to each client. 然后,您将迭代它以向每个客户端发送消息。 With that design, you wouldn't need a singleton server (just some good thread safety around the list). 使用该设计,您不需要单一服务器(只需在列表周围提供一些良好的线程安全性)。

2) If the clients close their server proxies, the server will not be able to send them any messages. 2)如果客户端关闭其服务器代理,服务器将无法向其发送任何消息。 You need to keep the proxy open and stash them somewhere in the client. 您需要保持代理打开并将其存储在客户端的某个位置。 This design will of course significantly limit scalability. 这种设计当然会显着限制可扩展性。

By default each client will get its own instance of the service. 默认情况下,每个客户端都将获得自己的服务实例。 You can however make your service a singleton (which will than process requests from all client). 但是,您可以使您的服务成为单一服务(这将处理来自所有客户端的请求)。

You may also want to skim through this Instance management article 您可能还想浏览此实例管理文章

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用双工wcf服务将消息分发给所有客户端? - How to use a duplex wcf service to distribute messages to all clients? 如何在没有崩溃应用程序的情况下通过WCF双工服务发送消息? - How to send message through WCF duplex service without crash application? 如何通过使用WCF NetHttpBinding(WebSockets)的服务器将一个客户端发送的消息广播(推送)到所有客户端? - How to broadcast (push) a message send by one client, to all clients through a server using WCF NetHttpBinding (WebSockets)? 如何通过Web API向所有客户端广播消息 - How do i broadcast a message to all clients via Web API 使用WCF回调时,客户端如何知道服务器是否已中断 - When using WCF callbacks how do clients know if the server has been interrupted WCF的聊天室功能,双工回调与轮询? - Chatroom functionality with WCF, duplex callbacks vs polling? 使用WCF Duplex回调更新JQuery进度条? - Update a JQuery progressbar using WCF Duplex callbacks? 我可以使用WCF双工绑定来中继消息吗? - Can I use WCF duplex binding to relay message? 具有来自服务的不同数据的WCF Duplex客户端 - WCF Duplex clients with different data from the service 如何在NET Core 2.0的SignalR alpha上向所有客户端发送消息 - How to send a message to all clients on SignalR alpha for NET Core 2.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM