Comments (3)
There are several reasons, but the main one is that CancellationToken is usually unavailable as a parameter to methods defined on the server side. The basic idea of TypedSignalR.Client is that the server-side and client-side share the same hub and receiver interface. So, for example, the problem occurs in the following cases.
public interface IHub
{
// Use CancellationToken.
// Note that you cannot actually write code like this.
Task<int> Add(int x, int y, CancellationToken cancellationToken);
}
// client-side
// This appears to be an unproblematic syntax.
// Note that you cannot actually write code like this.
var hubProxy = hubConnection.CreateHubProxy<IHub>(cancellationTokenSource.Token);
var result = await hubProxy.Add(7, 99, cancellationTokenSource.Token);
// server-side
public class MyHub : Hub, IHub
{
// SignalR Hub cannot handle this cancellationToken.
public Task<int> Add(int x, int y, CancellationToken cancellationToken)
{
}
}
On the other hand, server-to-client streaming can handle CancellationToken on the server-side, so it is possible to write code like the following
public interface IStreamingHub
{
// server-to-client streaming
IAsyncEnumerable<Person> GetStream(CancellationToken cancellationToken);
}
// client-side
// This code can be written.
var hubProxy = hubConnection.CreateHubProxy<IStreamingHub>(cancellationTokenSource.Token);
var stream = hubProxy.GetStream(cancellationTokenSource.Token);
from typedsignalr.client.
I see, that's unfortunate... Thank you!
from typedsignalr.client.
Note that if you call InvokeAsync
on the SignalR client, pass a CancellationToken, and then cancel the token, the cancellation will not propagate to the server-side. It only makes sense if you cancel before writing the serialized message to the connection is done.
If you want to confirm that the cancellation does not propagate to the server-side, try the following example code.
public class UnaryHub : Hub
{
private readonly ILogger<UnaryHub> _logger;
public UnaryHub(ILogger<UnaryHub> logger)
{
_logger = logger;
}
public override Task OnConnectedAsync()
{
_logger.Log(LogLevel.Information, "OnConnectedAsync");
return base.OnConnectedAsync();
}
public override Task OnDisconnectedAsync(Exception? exception)
{
_logger.Log(LogLevel.Information, "OnDisconnectedAsync");
return base.OnDisconnectedAsync(exception);
}
public async Task<int> Add(int x, int y)
{
await Task.Delay(TimeSpan.FromSeconds(10), this.Context.ConnectionAborted);
_logger.Log(LogLevel.Information, "UnaryHub.Add");
return x + y;
}
}
var connection = new HubConnectionBuilder()
.WithUrl("http://localhost:5105/Hubs/UnaryHub")
.Build();
try
{
await connection.StartAsync();
var cts = new CancellationTokenSource();
Console.WriteLine("InvokeAsync: Add");
var task = connection.InvokeAsync<int>("Add", 22, 77, cts.Token);
await Task.Delay(TimeSpan.FromSeconds(2));
cts.Cancel();
var result = await task;
Console.WriteLine($"result {result}");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.WriteLine("waiting");
await Task.Delay(TimeSpan.FromSeconds(10));
Console.WriteLine("StopAsync");
await connection.StopAsync();
Console.ReadLine();
from typedsignalr.client.
Related Issues (15)
- Support streaming
- Support client results (.NET 7) HOT 4
- Rethinking accessibility
- Add a streaming section to the README.md
- CreateHubProxy: using the generic parameter instead of interface HOT 1
- IHub does not inherit methods HOT 3
- CreateHubProxy does not support generic HOT 1
- Registering from a razor page HOT 2
- Source code not generated HOT 4
- Generated types are ignoring nullable option for arguments HOT 2
- Escaped reserved identifiers cause issues in generated code HOT 2
- Add ClientFactory Functionality HOT 1
- Usage in Unity HOT 7
- Nullable as parameter gives CS8639 HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typedsignalr.client.