[英]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#首选),客户端计算答案,然后返回结果。
(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.