From the course: Asynchronous Programming in C#
Solution: Cancel a slow HTTP request - C# Tutorial
From the course: Asynchronous Programming in C#
Solution: Cancel a slow HTTP request
(lively music) - [Instructor] The solution to the challenge really begins with the code you hopefully wrote for the challenge in the previous chapter. I'll briefly walk you through this in case you need a quick refresher on it. At the top, I wrote an async function named PrintWebpageContents that accepts a URL as a string parameter. I declared the function with the async keyword and specified that it will return a task. Inside the function, I create a new HttpClient instance and then call the GetStringAsync method on it, passing it the url parameter. I await the results of that asynchronous function and capture the webpage contents in a variable that I then output to the screen. A little further down, I wrote a few lines to call the function, passing it the URL for the LinkedIn Learning website. The part of this challenge you didn't do in the earlier one is adding a CancellationToken to cancel the request if it takes too long. Let's now write the additional code to implement that. At the top of the function body, I'll declare a new CancellationTokenSource variable and assign it a new CancellationTokenSource instance. Immediately after that, I'll call the CancelAfter instance method on the variable, and pass it 5000, the number of milliseconds that should trigger the cancellation. I can then use the CancellationTokenSource to pass a CancellationToken to the GetStringAsync method below. Remember I mentioned that there's an overload of it that takes a CancellationToken as a second parameter. I'll pass it here by referencing the Token property on the CancellationTokenSource variable. Now, in order to properly handle the exception that will be thrown as a result of a cancellation, I need to wrap this code in try-catch blocks. The canceled task will throw an OperationCanceledException, so I'm going to explicitly catch that type and then output a message to the console that the operation timed out, and include the text of the message property on the exception. We're almost done, but there's one more important piece I need to add. Remember that CancellationTokenSource implements IDisposable, so I need to make sure I call its Dispose method. I'll do that inside a finally block at the end. We can use the code at the bottom to call the function just as it is, so let's run this and see how it behaves. In the VS Code terminal, I'll start it with the command dotnet run. After a few seconds it starts to run, and we can tell the request completed before the five-second timeout, because we can see the contents of the webpage start to scroll past. Just so I can observe a cancellation, I'll go back to the code, and this time have it time out after 500 milliseconds instead of 5,000. I'll start the program again. After a few seconds, you can see that the cancellation did happen and we got the message from our catch block that the operation was canceled.