.Net Core GetConnectionString() returns null

This issue could be due to different reasons. My experience is more of related to configuration. Surprisingly everything was working with .net core 2.1 but was not working with .net core 2.2.

If you have a configuration something like below, the culprit line is having UseContentRoot() method

WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory()) //<== This is the culprit
.UseStartup<Program>()

The correct path which should be passed is the path where appsettings.json sits. You can solve this issue in two slightly different ways. Both solutions need to do one more step (see Common step)

Solution #1

Just update the UseContentRoot() method calling as follows. The same solution suggested for the issue# 589 by alexsandro-xpt

.UseContentRoot(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location))

Solution #2

At the first line of Main set current directory and leave the original configuration as it is. It is suggested as issue# 2239

// In Main
public static void Main(string[] args)
{
var rootPath = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
Directory.SetCurrentDirectory(rootPath);
......
}

The Common Step for both solutions

The above solutions are both already available in some form but it didn’t work until I did this additional changes.

Just make sure appsettings.json file is copied to corresponding bin folder (for Development) or deployment folder (for deployment) from where the application is running.

Change Copy to Output Directory to Copy always

Right Click appsettings.json =>Properties=>Copy always

Hope this works