dotnet / dotnet/AspNetCore.Docs
SignalR: Request Timeout for Client-Result Requests
- Dominant language
- C#
- Stars
- 13.1k
- Forks
- 24.6k
- Avg merge
- 1d 3h
- Merged PRs (30d)
- 97
Description
I searched all [SignalR Docs](https://learn.microsoft.com/en-us/aspnet/core/signalr/introduction?view=aspnetcore-7.0) and the github issues ([aspnetcore](https://github.com/dotnet/aspnetcore/issues?q=signal+timeout+cancellationtoken) and [AspNetCore.Docs](https://github.com/dotnet/AspNetCore.Docs/issues?page=1&q=signalr+timeout)) but I didn't find any description how to set a timeout for a client-result request.
I'd expect the topic "Request Timeout" to be covered somewhere in the docs. E.g. [here](https://learn.microsoft.com/en-us/aspnet/core/signalr/hubs?view=aspnetcore-7.0#client-results)
I suggest extending the following code snippet:
```csharp
public interface IClient
{
Task GetMessage();
}
public class ChatHub : Hub
{
public async Task WaitForMessage(string connectionId)
{
using var requestTimeout = new CancellationTokenSource(TimeSpan.FromSeconds(30));
string message = await Clients.Client(connectionId).GetMessage(requestTimeout.Token);
return message;
}
}
```
I noticed that passing a `CancellationToken` works as expected. If the timeout elapses before the request has completed, an `Exception` with the message `Canceled` is thrown.
If this is not the recommended approach, please describe the official way to set a request timeout
Contributor guide
Assessment
This issue has not been assessed yet.