给客户打电话时应​​signalr服务器端方法是异步? [英] Should signalr server-side methods be async when calling Clients?

查看:137
本文介绍了给客户打电话时应​​signalr服务器端方法是异步?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我继SignalR教程上:<一href=\"http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server\">http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server

I'm following the "SignalR Tutorial" on : http://www.asp.net/signalr/overview/hubs-api/hubs-api-guide-server

因此​​,让我们假设这个简单的聊天方式:

So let's assume this simple Chat Method :

public void Send(string name, string message)
{
     // Call the addNewMessageToPage method to update clients.
     Clients.All.addNewMessageToPage(name, message);
}

假设我有一个聊天室50 000用户。会有改变发送方法的任何好处是异步,是这样的:

Suppose that I have a Chat room with 50 000 users. Would there be any benefit of changing the Send method to be async, like this :

 public async Task Send(string name, string message)
 {
     // Call the addNewMessageToPage method to update clients.
     await Clients.All.addNewMessageToPage(name, message);
 }


  • 威尔IIS保持当前请求(谁出版了用户的
    聊天),并等到每个客户端通知?

  • 是调用客户引擎盖下完全异步和请求
    在这一点上公布?

  • 感谢您!

    推荐答案

    您应该等待任务的唯一原因是,当你使用scaleout。默认情况下,因为操作是如此之快没有一点使得它异步,但你可以,如果你想在内存中的消息总线返回完成的任务。要回答你的问题:

    The only reason you should await the task is when you're using scaleout. By default, the in memory message bus returns a completed task because the operation is so fast there's no point making it async but you can if you want to. To answer your questions:


    • 我们不会发送给客户相同的调用堆栈做方法调用上(例如Clients.All.addNewMessage没有任何东西,除了等待发布的消息总线)。无请求线程将等待客户端接收的任何东西(我们不支持等待客户端获取正常调用在SignalR消息上)。

    • We don't send to clients on the same call stack doing the method invocation (e.g. Clients.All.addNewMessage doesn't wait on anything except publishing to the message bus). No request thread will wait on clients to receive anything (we don't support waiting on the client getting the message in SignalR for normal invocations).

    它始终是异步,即使你没有在调用点使用的等待。我们有一个消息代理实际执行写入到客户端。当你调用该方法,你只是在缓冲区中放置一个消息。在未来的一段时间内,该消息被交付给客户。

    It is always async even if you don't use await at the call site. We have a message broker that actually does the writing to the clients. When you call that method, you're just putting a message in a buffer. At some time in the future, that message gets delivered to clients.

    在scaleout情况下,调用客户端的方法将消息发送到外部服务(SQL,Redis的,服务总线),而且可能会失败,所以你想,这样的例外观察等待任务。

    In the scaleout scenario, calling a client method sends a message to an external service (sql, redis, service bus) and that can fail so you want to await the task so that exceptions are observed.

    希望帮助

    这篇关于给客户打电话时应​​signalr服务器端方法是异步?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆