In case someone lands here on getting this error on a self built executable - for me, using dotnet publish --self-contained true instead of simple dotnet build solved the problem.
dotnet publish --self-contained true
dotnet build