question

babakfakhriloo avatar image
0 Votes"
babakfakhriloo asked Bruce-SqlWork answered

override default asp.net core cancelationtoken or change default timeout for requests.

In my asp.net core 5.0 app, I call an async method that might take some time to be processed.

 await someObject.LongRunningProcess(cancelationToken);

However, I want that method to be timeout after 5 seconds. I know that instead of "cancelationToken" passed by asp.net core action, I can use "CancellationTokenSource" :

 var s_cts = new CancellationTokenSource();
 s_cts.CancelAfter(TimeSpan.FromSeconds(5);
    
 await someObject.LongRunningProcess(s_cts );

Is it possible to use "CancellationTokenSource" as a default "Cancelation Token" policy for all asp.net core requests ? I mean override the one which is passed as a parameter of the action ?

Or is it possible to change the default timeout for all request in asp.net core 5.0 ?





dotnet-aspnet-core-webapi
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @babakfakhriloo,You cannot use c# code to set the timeout period, you can only achieve this requirement by setting web.config

0 Votes 0 ·

Hi @babakfakhriloo,I think this is feasible, you can try.

0 Votes 0 ·
rijwanansari avatar image
0 Votes"
rijwanansari answered
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Bruce-SqlWork avatar image
0 Votes"
Bruce-SqlWork answered

note: while you can set the cancellation token in middleware, all your actions still need to pass it to any async tasks they call. also be sure the tasks honor the cancellation token.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.