Exercise: Shipping API Timeout Fallback

Enforce timeouts on asynchronous operations to prevent application freezes and handle the resulting exceptions.

Problem statement

Your e-commerce checkout page fetches live shipping rates from a third-party logistics API. Sometimes, this API is extremely slow or unresponsive. To ensure the customer isn't stuck waiting forever, you must enforce a strict 2-second timeout on the request. If the API fails to respond in time, your utility must catch the timeout and return a standard flat shipping rate of $10.00 instead.

Task requirements

  • Call the simulated GetLiveShippingRateAsync method from within GetRateWithFallbackAsync.

  • Apply a 2-second timeout to the task.

  • If successful within the time limit, return the live rate.

  • If it times out, catch the error and return the fallback rate of $10.00.

Constraints

  • You must use the .WaitAsync(TimeSpan) method to enforce the timeout constraint.

  • You must wrap the await call in a try-catch block inside the utility class.

  • You must strictly catch TimeoutException, not the generic Exception base class.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Chain .WaitAsync(TimeSpan.FromSeconds(2)) directly to the GetLiveShippingRateAsync() call inside the try block.

  • Return the result of the await immediately if it succeeds.

  • In the catch (TimeoutException) block, return the string "$10.00".

Exercise: Shipping API Timeout Fallback

Enforce timeouts on asynchronous operations to prevent application freezes and handle the resulting exceptions.

Problem statement

Your e-commerce checkout page fetches live shipping rates from a third-party logistics API. Sometimes, this API is extremely slow or unresponsive. To ensure the customer isn't stuck waiting forever, you must enforce a strict 2-second timeout on the request. If the API fails to respond in time, your utility must catch the timeout and return a standard flat shipping rate of $10.00 instead.

Task requirements

  • Call the simulated GetLiveShippingRateAsync method from within GetRateWithFallbackAsync.

  • Apply a 2-second timeout to the task.

  • If successful within the time limit, return the live rate.

  • If it times out, catch the error and return the fallback rate of $10.00.

Constraints

  • You must use the .WaitAsync(TimeSpan) method to enforce the timeout constraint.

  • You must wrap the await call in a try-catch block inside the utility class.

  • You must strictly catch TimeoutException, not the generic Exception base class.

Good luck trying the exercise! If you’re unsure how to proceed, check the “Solution” tab above.

Get hints

  • Chain .WaitAsync(TimeSpan.FromSeconds(2)) directly to the GetLiveShippingRateAsync() call inside the try block.

  • Return the result of the await immediately if it succeeds.

  • In the catch (TimeoutException) block, return the string "$10.00".

C# 14.0
using System.Threading.Tasks;
using Logistics;
Console.WriteLine("Calculating shipping costs...");
Console.WriteLine("(Run multiple times to see both success and timeout behaviors)");
ShippingCalculator calculator = new ShippingCalculator();
string finalRate = await calculator.GetRateWithFallbackAsync();
Console.WriteLine($"Shipping rate applied: {finalRate}");