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

Friday, December 31, 2021

Configure HttpClient to consume windows authenticated NTLM service in ASP.NET Core

Recently I migrated a system to Linux based docker platform from IIS environment. I had a service call to some other API that has NTLM authentication.

That was working fine by just setting Credentials to HttpClient when hosted in IIS. 

But when you run dotnet app in Linux you do not have that windows authentication fancy feature. 

Below is how I managed to configure HttpClient with NTLM Authentication using CredentialCache.


Monday, May 24, 2021

How to update only one field using EF Core?

This method is useful When you are trying to update some record in the table while you are not tracking that entity. 

If you are using tracking then EF is smart enough to update only certain fields. 

Below is the code.


Wednesday, January 27, 2021

Fix Openshift build pod was killed due to an out of memory condition

I was doing a large build using oc build command in Openshift dedicated cluster. This build has many steps in Dockefile and it fails somewhere in copying blobs. Error was "The build pod was killed due to an out-of-memory condition.". 

I found two solutions for this. 

1. Increase build pod resources.
resources: 
 requests: 
   cpu: "100m" 
   memory: "1024Mi" 

2. Build locally using docker build and push the image to the image stream.

- Build locally
docker build -t myapi 

- Tag build
docker tag myapi default-route-openshift-image-registry.apps.ca-central-1.starter.openshift-online.com/apis/myapi

- Login to an openshift registry
docker login default-route-openshift-image-registry.apps.ca-central-1.starter.openshift-online.com -u $(oc whoami) -p $(oc whoami -t)

- Push the image to the registry
docker push default-route-openshift-image-registry.apps.ca-central-1.starter.openshift-online.com/apis/myapi

Thursday, December 31, 2020

Run Docker With Visual Studio in Corporate Machine with VPN or Firewall

I was trying to run some API using docker with visual studio docker tools. 

Idea behind that is I will able to attach docker process to visual studio and do some tweaks very easily using visual studio docker tools. (Haha I am lazy).

But unfortunately, my company laptop is heavily protected with no Administrator, a bunch of security tools, and a VPN also. 

Some errors I got with various tries.

Debugger path 'C:\User\...\vs2017u5' for DockerFile is invalid. 

One or more errors occurred.

Failed to launch debug adapter. Additional information may be avaliable in the output window.

Building the project is successful but errors out on launch.

The program '' has exited with code -1 (0xffffffff).

\vsdbg\vs2017u5' for Dockerfile.... Container.targets Dockerfile is invalid

In order to work this normally, there is a power shell script provided by Microsoft to download software, .NET Core Debugger from Microsoft, aka vsdbg.

https://aka.ms/getvsdbgsh

This will download a zip file and will be created in the below directory with debugger files. 

C:\Users\Chathuranga\vsdbg\vs2017u5.


With the firewall and VPN, my laptop couldn't be able to do this in a proper way. 


Time to read what is inside of getvsdbg.sh.


You will find a method like this download_and_extract()

This method constructs the download URL.


url="https://vsdebugger.azureedge.net/vsdbg-${target}/${vsdbgCompressedFile}"


Time to determine parameters.


Check this method in SH file, 


set_vsdbg_version

        latest)

            __VsDbgVersion=16.8.11013.1

            ;;

        vs2019)

            __VsDbgVersion=16.8.11013.1

            ;;

        vsfm-8)

            __VsDbgVersion=16.8.11013.1

            ;;

        vs2017u5)

            __VsDbgVersion=16.8.11013.1

            ;;

        vs2017u1)

            __VsDbgVersion="15.1.10630.1"


Decide what is the version you want from this. 


I selected 16.8.11013.1 because I have vs 2109 16.8.3


Next, decide which vsdbg configuration you want. 

ex: debian.8-x64, linux-musl-x64, linux-x64.


In this case, I selected linux-x64, the reason is I wanted to debug. 

If you just want to run then select linux-musl-x64, it is the runtime


The final URL will be like below

https://vsdebugger.azureedge.net/vsdbg-16-8-11013-1/vsdbg-linux-x64.zip


Download this and extract it to C:\Users\XXX\vsdbg\vs2017u5.

Create vsdbg folder if it does not exist. and keep in mind that do not extract to a subfolder.


Ok, that is it. Enjoy debugging.