Giter Club home page Giter Club logo

Comments (3)

nenoNaninu avatar nenoNaninu commented on May 30, 2024

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.

ADIX7 avatar ADIX7 commented on May 30, 2024

I see, that's unfortunate... Thank you!

from typedsignalr.client.

nenoNaninu avatar nenoNaninu commented on May 30, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

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

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.