.Net 1 Flashcards
How to combine two cancellation tokens in one?
You can use
public static CancellationTokenSource CreateLinkedTokenSource(
CancellationToken token1,
CancellationToken token2)
method and get a token from the CancellationTokenSource.
Example:
var combinedCancelationToken = CancellationTokenSource.CreateLinkedTokenSource(
startupCancellationToken, applicationLifetime.ApplicationStarted).Token;
combinedCancelationToken will be canceled if one of the source tokens was canceled.
How you can wait until the hosted application is started?
You can use IApplicationLifetime.ApplicationStarted property. The return type of the property is CancellationToken. So when it’s canceled it means the application is started.
Example:
await Task.Delay(Timeout.InfiniteTimeSpan, applicationLifetime.ApplicationStarted)
.ContinueWith(_ => { }, TaskContinuationOptions.OnlyOnCanceled) // to avoid OperationCanceledException.
What is the difference between UseRouting and UseEndpoints?
UseRouting: Matches request to an endpoint.
UseEndpoints: Execute the matched endpoint.
It decouples the route matching and resolution functionality from the endpoint executing functionality. This makes the ASP.NET Core framework more flexible and allows other middlewares to act between UseRouting and UseEndpoints
Where in pipeline do you have to call UseAuthentication?
Between UseRouting and UseEndpoints.
So that route information is available for authentication decisions and before UseEndpoints so that users are authenticated before accessing the endpoints.
Do you need to explicetely call UseRouting.
Yes, for .net core.
But in.net 6 you don’t, you can do it explicetely to move routing matching to another step in pipeline, by default it’s the first one.