简体   繁体   English

具有返回值的WCF服务器到客户端回调方法

[英]WCF Server-to-Client callback method with return value

I've attempted the question different ways, check my profile for the other two questions explaining the difficulty I've had with the approaches I've taken to this scenario. 我尝试了不同的问题,检查我的个人资料中的其他两个问题,解释了我对这种情况采取的方法遇到的困难。 I'll skip them here. 我会在这里跳过它们。

I just need an example (C# preferred) of a server calling back to a client (over a nettcp channel), the client calculates an answer, and returns a result. 我只需要一个服务器回调客户端(通过nettcp通道)的示例(C#首选),客户端计算答案,然后返回结果。

  • The server's calling thread is blocked until the client sends the response. 服务器的调用线程被阻塞,直到客户端发送响应。
  • The server's callback is prompted by some event, such as a timer, not a call by the client. 服务器的回调由某些事件提示,例如计时器,而不是客户端的调用。
  • The client does not need to call back to the server within the context of the server's callback, so there should be no deadlocking challenges. 客户端不需要在服务器回调的上下文中回调服务器,因此不存在死锁挑战。
  • The client may take (and for this exercise, should take) one or more parameters with which to perform the calculation 客户可以采取(并且在本练习中应该采用)用于执行计算的一个或多个参数
  • The client may return (and for this exercise, should return) a non-void result 客户可以返回(并且为此练习,应该返回)非空的结果
  • The approach used should at least be compatible, if not implemented, with the ability to handle multiple clients in turn, including a subscribe() and unsubscribe() functionality. 如果不实现,所使用的方法至少应该兼容,能够依次处理多个客户端,包括subscribe()和unsubscribe()功能。
  • This is not homework, it is to serve as an example of how to build a WCF-based subscriber/publisher server that supports (few) trusted clients with low latency server-client communication without polling and without throwing messages (over the fence) 这不是功课,它是作为如何构建基于WCF的订户/发布者服务器的示例,该服务器支持(少数)具有低延迟服务器 - 客户端通信的可信客户端,而无需轮询和不丢弃消息(通过围栏)
  • I am specifically NOT interested in solutions that involve (IsOneWay = true) , unless it becomes clear that I am very confused about its meaning and its consequences. 我特别对涉及(IsOneWay = true)解决方案不感兴趣,除非我很清楚我对它的含义及其后果非常困惑。

Thanks! 谢谢!

Check this article on CodeProject. 在CodeProject上查看这篇文章 This describes basic example of callbacks. 这描述了回调的基本示例。 Few things that you may have to change: 你可能不得不改变几件事:

  • On callback contract, operations marked as one way - this is to avoid blocking of server due to bad client (a recommended practice). 在回调合同上,操作标记为一种方式 - 这是为了避免由于客户端不良而阻塞服务器(建议的做法)。 But if you must block the server then you need to remove one way. 但是如果你必须阻止服务器,那么你需要删除一种方法。 Note that if you are going to callback multiple clients one by one then you may have to callback each one on different thread other wise first client will block callback to next client. 请注意,如果您要逐个回调多个客户端,那么您可能必须在不同的线程上回调每个客户端,否则第一个客户端将阻止回调到下一个客户端。

  • When to invoke callback is really a server implementation. 何时调用回调实际上是一个服务器实现。 The given example maintains a list of client callback channels whenever client joins (or subscribes in your requirement). 每当客户端加入(或订阅您的需求)时,给定的示例都会维护一个客户端回调通道列表。 Now this list can be used to invoke callback in any way you want. 现在,此列表可用于以您想要的任何方式调用回调。 So you can invoke callbacks on timer by simply iterating over the list. 因此,您可以通过简单地遍历列表来调用计时器上的回调。 Note that you have to ensure thread-safe access to the list. 请注意,您必须确保对列表的线程安全访问。

  • If client has to return some result in callback then again OneWay cannot be used. 如果客户端必须在回调中返回一些结果,则不能再使用OneWay。

  • As mentioned earlier, subscribe means simply adding to the list (join party in example) and unsubscribe means removing from the list (leave party). 如前所述,订阅仅意味着添加到列表(示例中为加入方),取消订阅意味着从列表中删除(离开方)。

Edit : 编辑

I have taken the source code from the example sighted and modified it as follows: 我从看到的示例中获取了源代码,并将其修改如下:

Added a method Echo in callback contract: 在回调契约中添加了一个方法Echo

public interface IBeerInventoryCallback
{
   ...

    [OperationContract]
    string Echo(string message);
}

Invoked Echo from service when someone left the party and printed response from client on console. 当某人离开聚会并在控制台上打印来自客户端的响应时,从服务中调用Echo。 And it worked w/o any issues. 它没有任何问题。

Note that this example uses VS generated client proxy that inherits from System.ServiceModel.DuplexClientBase<T> which makes client code much simpler. 请注意,此示例使用VS生成的客户端代理,该代理继承自System.ServiceModel.DuplexClientBase<T> ,这使客户端代码更加简单。 Perhaps, you should try it. 也许,你应该尝试一下。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM