izengod
izengod

Reputation: 1156

Unable to read details from appsettings.json

I'm unable to retrieve the value from appsettings.json, when I run the code below, I get an error

System.NullReferenceException: 'Object reference not set to an instance of an object.

appSettings.json

"MySettings": {
"ConnectionString": "",
"Provider": "Microsoft.EntityFrameworkCore.SqlServer" }

Startup.cs

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        Configuration = builder.Build();
    }


    public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddMvc();
        services.Configure<MySettings>(Configuration.GetSection("MySettings"));
    }

MySettings.cs

public class MySettings 
{
    public static string ConnectionString { get; set; }

    public static string Provider { get; set; }
}

TestDriveDatabase.cs

public class TestDriveDatabase
{
    private IDatabase objDLOperation;

    public TestDriveDatabase(string strConnectionstring)
    {
        objDLOperation = GetDataLayerInstance(strConnectionstring);
    }
    public IDatabase GetDataLayerInstance(string strConnectionstring)
    {
        IDatabase objInstance = null;
        var provider = MySettings.Provider;

        if (provider == "Microsoft.EntityFrameworkCore.SqlServer")
        {
            objInstance = new SQLDatabase(strConnectionstring);
        }
        else if (provider == "Npgsql.EntityFrameworkCore.PostgreSQL")
        {
            objInstance = new PostgreDatabase(strConnectionstring);
        }

        return objInstance;
    }
}

Unfortunately the mapping from appsettings.json to MySettings is not happening and the variable provider always remains null. Whats wrong here ?

Upvotes: 0

Views: 1270

Answers (1)

Tseng
Tseng

Reputation: 64307

First remove the static keyword, you can't map configuration values to static properties

public class MySettings 
{
    public string ConnectionString { get; set; }

    public string Provider { get; set; }
}

Second, inject MySettings into your TestDriveDatabase.

public class TestDriveDatabase
{
    private IDatabase objDLOperation;
    private readonly MySettings settings;

    public TestDriveDatabase(IOptions<MySettings> mySettings)
    {
        this.settings = mySettings.Value;
        objDLOperation = GetDataLayerInstance(strConnectionstring);
    }
    public IDatabase GetDataLayerInstance(string strConnectionstring)
    {
        IDatabase objInstance = null;
        var provider = settings.Provider;

        if (provider == "Microsoft.EntityFrameworkCore.SqlServer")
        {
            objInstance = new SQLDatabase(settings.Connectionstring);
        }
        else if (provider == "Npgsql.EntityFrameworkCore.PostgreSQL")
        {
            objInstance = new PostgreDatabase(settings.Connectionstring);
        }

        return objInstance;
    }
}

Upvotes: 1

Related Questions