Sunday, November 20, 2022

Resilient HTTP client with Polly and Flurl

Having a resilient HTTP client is a huge benefit if your application depends on third-party API calls. This will helps to avoid transient errors. These errors may be temporary and will be solved quickly. Most of the time these errors occurred due to network issues. 

The below example demonstrates how to implement a HttpClient with multiple retries using Polly and Flurl

Polly is a .NET resilience and transient-fault-handling library that allows developers to express policies such as Retry, Circuit Breaker, Timeout, Bulkhead Isolation, Rate-limiting, and Fallback in a fluent and thread-safe manner.

Flurl is a modern, fluent, asynchronous, testable, portable, buzzword-laden URL builder and HTTP client library.

Creating the Retry Policy 

This IsWorthRetrying method will check returned HTTP status is worth of retry.

And I used  OnRetryAsyncFunc to log the retry information. 


Use the Retry Policy

Example usage and results

var result = await apiClient.GetAsync("https://httpstat.us/503").ConfigureAwait(false);
Console.WriteLine($"Hello, World! {result}");
 

Full implementation 

Sunday, November 6, 2022

Change PowerShell mode to FullLanguage mode from ConstrainedLanguage

You may face this issue if you are running under an enterprise laptop and your administrator put you into constrained mode.

If you want to run software like docker, then you need to use FullLanguage mode.

Use the below command to check your language mode.

$ExecutionContext.SessionState.LanguageMode

There are two ways to set it to FullLanguage mode.

1. Using the below command

$ExecutionContext.SessionState.LanguageMode = 'fulllanguage'

2. Edit your registry __PSLockdownPolicy value to 8.

HKLM\System\CurrentControlSet\Control\SESSION MANAGER\Environment\__PSLockdownPolicy