Skip to main content
 首页 » 编程设计

c#之单向操作与异步调用

2024年02月20日13cloudgamer

如 first section of this article 中所述(.chm 文件 - 请参阅下面的注释)

One-way calls do not equate to asynchronous calls. When one-way calls reach the service, they may not be dispatched all at once and may be queued up on the service side to be dispatched one at a time... If the number of queued messages has exceeded the queue's capacity, then the client will block, even when issuing a one-way call.

如果调用单向调用,它何时将控制权返回给调用者?同一篇文章的介绍指出,单向调用 用于即发即弃操作,从而模拟一种异步调用。 如果 session WCF 服务有一个启动 session 的 Login() 单向方法,该方法何时将控制权返回给调用者?它是否在该方法实际在服务上执行之前返回?如果是这样,我如何确定该方法是在服务上执行的?

如果我希望服务向客户端返回登录过程中可能发生的错误,异步调用是否是实现即发即弃行为的唯一方法?


注意:上面的引述摘自 Juval Lowy 的一篇名为 WCF Essentials - What You Need To Know About One-Way Calls, Callbacks, And Events 的文章,可以在 10 月找到2006年MSDN杂志。上面的链接指向 MSDN 上的帮助文件 (.chm) 格式。如果您无法使用链接的 CHM 文件(我不能),您可以使用 7zip 打开它。

请您参考如下方法:

If a sessionful WCF service has a Login one-way method which starts the session, when does this method return control to the caller?

对于 OneWay 调用:一旦服务调用被分派(dispatch)或排队,调用者将继续执行。基本上一旦服务成功接收到调用。

how can I be sure that the method was performed on service?

如果客户端没有得到异常,则服务调用已经(或将)执行。客户端不知道服务调用何时实际运行,或者它是成功还是抛出异常。

If I wanted the service to return to the client the possible errors that occur during the login procedure, would be an asynchronous call the only way to achieve a fire-and-forget behaviour?

这没有意义。即发即弃的定义意味着忘记结果。它不会向客户端返回任何值或错误。但是,异步调用不是即发即弃的,会返回错误。