Q-bertsuit
Q-bertsuit

Reputation: 3437

Async/await in a signalR hub

I'm trying to understand the impact of using async/await in a signalR hub. 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? Would invoking either of these be a an awaitable operation for a client?

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);
        }

    }
}

Upvotes: 0

Views: 633

Answers (1)

Stephen Cleary
Stephen Cleary

Reputation: 456417

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?

Yes. Similar to ASP.NET WebApi, the response is not sent until the task completes.

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

Yes. Since there is a network (I/O) between the client and server, they can be implemented synchronously or asynchronously, and either implementation can be called synchronously or asynchronously.

Upvotes: 3

Related Questions