-1

I created a default Blazor WASM ASP.NET hosted app.

On WeatherForecastController Get method i added the parameter CancellationToken cancellationToken:

[HttpGet] public IEnumerable<WeatherForecast> Get(CancellationToken cancellationToken)

Now, from client the http call is Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast", CancellationToken.None) so i expect on server side who the property cancellationToken.CanBeCanceled is false BUT the property is true!

I also tried: Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast") or Http.GetFromJsonAsync<WeatherForecast[]>("WeatherForecast", default(CancellationToken))

but on server the CanBeCanceled property is always true

Why?

How to have a default cancellationToken with CanBeCanceled = false?

Thanks

Claudio
  • 133
  • 2
  • 11

1 Answers1

0

It's not the same cancellation token. There's no concept in HTTP of cancellation (at all), let alone some specific facility to pass a .NET cancellation token.

The one on the server side represents "the client disconnected"1 and that's always a possibility.

How to have a default cancellationToken with CanBeCanceled = false?

How could that ever be an actual requirement? There's no way that the client can guarantee that it will maintain its connection and be able to receive a response the server generates.


1And the one passed to the HttpClient is essentially "please disconnect if this gets cancelled", or the HTTP 2 equivalent of resetting one stream.

Damien_The_Unbeliever
  • 234,701
  • 27
  • 340
  • 448
  • Thanks @Damien_The_Unbeliever, my goal is to provide a default implementation who not require test for cancellation request on server. So if the client does not pass the cancellationToken to the server or pass CancellationToken.None , the server can have cancellationToken.CanBeCanceled = false and not handle the cancellation. On the other side, if client pass a valid cancellationToken the server will have cancellationToken.CanBeCanceled = true and implement the logic to cancel the current task. – Claudio Oct 19 '21 at 11:12
  • I can reach my goal using a nullable CancellationToken, but is a little more inconvenient because i have to test for null and cast the object or use the nullable Value property. The CancellationToken class offer out of the box the CanBeCanceled property to solve my problem, but seem it's always true for http requests.. – Claudio Oct 19 '21 at 11:41
  • 1
    @Claudio - like I said, it's not the same cancellation token. The one the server gets is one supplied by the framework to indicate that the client has disconnected. You have no way of knowing if the client even supplied a cancellation token in its use of `HttpClient` methods. – Damien_The_Unbeliever Oct 19 '21 at 12:04