Should signalr server-side methods be async when calling Clients?

后端 未结 1 1413
清歌不尽
清歌不尽 2021-02-02 09:11

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 :



        
相关标签:
1条回答
  • 2021-02-02 09:47

    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:

    • 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.

    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.

    Hope that helps

    0 讨论(0)
提交回复
热议问题