简体   繁体   中英

Issues reading appsettings.json file C# .NET Core

I'm coming from regular .NET Web API and I have found the experience around configuration in .NET Core 2 absolutely maddening.

I have read the official documentation and a few tutorials such as this one , but they all see to error.

I have a Database helper class that is supposed to establish a MongoDB connection.

My configuration is rather simple

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "mongodb://localhost:27017"
  }
}
  • I started with the .NET Core official Angular (ngx) boilerplate template
  • I add the configuration as a service singleton in Startup.CS under the ConfigureServices section like so services.AddSingleton(Configuration);
  • I attempt to inject the configuration into my class like so

     public class DatabaseHelper { public static string connstring { get; private set; } public DatabaseHelper(IConfiguration Configuration) { connstring = Configuration["ConnectionStrings:DefaultConnectionString"]; } public static IMongoDatabase QuizDB { get; } = GetDatabase("QuizEngine"); public static IMongoCollection<Quiz> QuizCol { get; } = GetQuizCollection(); public static MongoClient GetConnection() { return new MongoClient(connstring); } public static IMongoDatabase GetDatabase(string database) { MongoClient conn = DatabaseHelper.GetConnection(); return conn.GetDatabase(database); } public static IMongoCollection<Quiz> GetQuizCollection() { return DatabaseHelper.QuizDB.GetCollection<Quiz>("Quizzes"); } } 

This compiles and builds fine with no Intellesense errors - But when I step through it in the debugger, the connstring is null. I have tried playing around with the configuration names etc, but I always seem to come up empty.

I have also tried the POCO way using the example code in the below answer, but I seem to run into static field initializer issues.

Getting value from appsettings.json in .net core

if it helps here is the the startup class part where it sets the public value of Configuration

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

You don't need to add the configuration as a service singleton in Startup.CS under the ConfigureServices section like services.AddSingleton(Configuration);

If you separate your solution into multiple projects with use of class libraries, Microsoft.Extensions.Options.ConfigurationExtensions package comes in handy for reading the values from appsettings files and injecting them into your configuration classes within projects.

It has 2 extensions you can use:

public static T Get<T>(this IConfiguration configuration);
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, 
    IConfiguration config) where TOptions : class;

your configuration file appsetting.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "MongoDBConnection": "mongodb://localhost:27017"
  }
}

add code for ConfigureServices method in startup.cs file

  services.Configure<MongoDBconfig>(Configuration.GetSection("ConnectionStrings"));
  services.AddSingleton<DatabaseHelper>();

your DBhelper.cs

    public class MongoDBconfig
    {
        public string MongoDBConnection { get; set; }
    }

    public class DatabaseHelper
    {
        public static string connstring { get; private set; }
        public DatabaseHelper(IOptions<MongoDBconfig> Configuration)
        {
            connstring = Configuration.Value.MongoDBConnection;
        }


        public static IMongoDatabase QuizDB { get; } = GetDatabase("QuizEngine");
        public static IMongoCollection<Quiz> QuizCol { get; } = GetQuizCollection();


        public static MongoClient GetConnection()
        {

            return new MongoClient(connstring);
        }

        public static IMongoDatabase GetDatabase(string database)
        {

            MongoClient conn = DatabaseHelper.GetConnection();
            return conn.GetDatabase(database);
        }

        public static IMongoCollection<Quiz> GetQuizCollection()
        {
            return DatabaseHelper.QuizDB.GetCollection<Quiz>("Quizzes");
        }
    }

Try the following way using this extension method GetConnectionString

public DatabaseHelper(IConfiguration Configuration)
{
    connstring = Configuration.GetConnectionString("DefaultConnection");
}

Try this

public DatabaseHelper(IConfiguration Configuration)
{
   string test = Configuration.GetSection("ConnectionStrings")["DefaultConnection"];
   Console.WriteLine(test);

}

Check if this able to extract the value in your appsettings.json

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM