1 I have a long task (3mn) triggered by a Js client to an Asp.Net Core SignalR Hub
It works fine :
public class OptimizerHub : Hub, IOptimizerNotification
{
public async Task Optimize(LightingOptimizationInput lightingOptimizationInput)
{
LightingOptimizer lightingOptimizer = CreateLightingOptimizer();
Task t = lightingOptimizer.Optimize(lightingOptimizationInput);
await t;
}
}
2 The server callbacks the client to notify progress, messages, ...
Clients.Caller.SendAsync(nameof(OnProgress), progress);
It works fine so far.
3 I want the task to be cancellable with a client call to a Hub method
public Task Cancel()
{
GetContextLightingOptimizer()?.Cancel();
return Task.FromResult(0);
}
4 The problem
When the client makes the call, I see it go to the server in Chrome developer tools detail. The call doesn't get to the server before the end long task ends (3mn) !
5 I have tried many solutions
Like changing my long task call method, always failing :
// Don't wait end of task, fails because the context disappear and can't call back the client :
// Exception : "Cannot access a disposed object"
public async Task Optimize(LightingOptimizationInput lightingOptimizationInput)
{
LightingOptimizer lightingOptimizer = CreateLightingOptimizer();
Task t = lightingOptimizer.Optimize(lightingOptimizationInput);
}
6 Possible solutions
The only solution I imagine right now is the client making an Http call in a Http controller, passing a connection id that could make the cancellation.
That post provides information about a possible solution : Call SignalR Core Hub method from Controller
7 Questions
Is there a simple way that the client makes a second call to the hub while a first call is being processed ?
There is also a post giving about concurrent calls : SignalR multiple concurrent calls from client
Should I deduce from the previous post that even if my hub server method can make calls many times to the client, it can't process any other call from the client ?