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.

Tuesday, December 8, 2020

How to build .NETCORE app using docker and get output

Below is the docker file that I am going to use for build. Then using below shell file, you can build and take out .dlls. how to run it $./dockerBuild.sh

Friday, November 6, 2020

Get SSL certificate using .NET Core and .NET

You can achieve this using HttpClientHandler Class and HttpClientHandler.ServerCertificateCustomValidationCallback Property. Sample code: Please add a reference to System.Security