Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to support CancellationToken #234

Merged

Conversation

NoahSurprenant
Copy link
Contributor

Hello, I was running into issues while attempting to generate my TypeScript when my receiver interface had a CancellatonToken parameter. Upon investigation, I only found this issue #87 but no solution was mentioned. In my use case the CancellationToken is found on the Receiver and not the Hub, and I am not streaming. I am opening this PR to allow the generation with CancellationToken, and updating the analyzer to ignore CancellationTokens found on the Receiver

@nenoNaninu
Copy link
Owner

Thanks for the PR. However, SignalR does not propagate CancellationToken from server to client. In what use case do we need the CancellationToke as a parameter to the receiver's method? It would be helpful if you show me examples.

@nenoNaninu
Copy link
Owner

Investigation showed that SignalR handles cases where the last parameter of a strongly typed caller's method is a CancellationToken specially.

public interface IHub
{
    Task M0();
}

public interface IReceiver
{
    Task M1(CancellationToken cancellationToken); // OK
    Task M2(int value, CancellationToken cancellationToken); // OK
    Task M3(CancellationToken cancellationToken, int value); // Serialization Error
}
public class MyHub : Hub<IReceiver>, IHub
{
    private readonly ILogger<MyHub> _logger;

    public MyHub(ILogger<MyHub> logger)
    {
        _logger = logger;
    }

    public async Task M0()
    {
        var cts = new CancellationTokenSource();

        try
        {
            await this.Clients.Caller.M1(cts.Token); // OK
            await this.Clients.Caller.M2(99, cts.Token); // OK
            await this.Clients.Caller.M3(cts.Token, 99); // Serialization Error
        }
        catch (Exception exception)
        {
            _logger.LogError(exception, "MyHub.M0");
        }
        finally
        {
            cts.Cancel();
            cts.Dispose();
        }
    }
}
HubConnection connection = ...;

var hub = connection.CreateHubProxy<IHub>();

connection.On("M1", () =>
{
    Console.WriteLine("M1");
});

connection.On<int>("M2", value =>
{
    Console.WriteLine($"M2: {value}");
});

await connection.StartAsync();

await hub.M0();

@nenoNaninu nenoNaninu changed the base branch from main to receiver_CancellationToken September 24, 2024 09:49
Copy link
Owner

@nenoNaninu nenoNaninu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR!

@nenoNaninu nenoNaninu merged commit bd94eb7 into nenoNaninu:receiver_CancellationToken Sep 24, 2024
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants