Async/await 在 signalR 集线器中

Async/await in a signalR hub

我试图了解在 signalR 集线器中使用 async/await 的影响。我是否正确地假设方法 GetAllStocksGetAllStocksAsync 就单个客户端而言是相同的,唯一的区别在于可伸缩性?调用其中任何一个都是客户端可等待的操作吗?

using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Microsoft.AspNetCore.SignalR;

namespace StockTickR.Hubs
{
    public class StockTickerHub : Hub
    {
        private readonly StockTicker _stockTicker;

        public StockTickerHub(StockTicker stockTicker)
        {
            _stockTicker = stockTicker;
        }

        public IEnumerable<Stock> GetAllStocks()
        {
            var result = _stockTicker.GetAllStocks();
 
            return DoSomethingWithResult(result);
        }

        public async Task<IEnumerable<Stock>> GetAllStocksAsync()
        {
            var result = await _stockTicker.GetAllStocksAsync();

            return DoSomethingWithResult(result);
        }

    }
}

Am I correct to assume that the methods GetAllStocks and GetAllStocksAsync are identical as far as a single client is concerned, and that the only difference lies in scalability?

是的。类似于ASP.NET WebApi,直到任务完成才发送响应。

Would invoking either of these be a an awaitable operation for a client?

是的。由于客户端和服务器之间存在网络(I/O),因此它们可以同步或异步实现,并且可以同步或异步调用任一实现。