0

I am migrating from .NET Core 5.0 to 6.0.

The following piece of code works just fine in 6.0.

builder.Services.AddDbContext<QuoteCMSContext>(options =>
        options.UseSqlite("Data Source=quotesdatabase.db"));

Now, in my old 5.0 project, I had something like this.

    //database context
    services.AddDbContext<QuoteCMSContext>(options =>
                options.UseSqlite(Configuration["SqliteConnectionString"]));

I looked at all these other questions, which appear to deal with my problem.

ASP.NET Core 6 how to access Configuration during startup

Getting value from appsettings.json in .net core

ConnectionString in .NET Core 6

Eventually, I came up with this.

builder.Services.AddDbContext<QuoteCMSContext>(options =>
        options.UseSqlite(builder.Configuration["SQLiteConnectionString"]));

and this

builder.Services.AddDbContext<QuoteCMSContext>(options =>
        options.UseSqlite(builder.Configuration.GetConnectionString("SQLiteConnectionString")));

in both cases, I keep getting null exceptions. I dont' know what I am missing.

Here is my appsettings.json, just in case.

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "SQLiteConnectionString": "Data Source=quotesdatabase.db",
  "AzureSqlServerConnectionString": "Server=tcp:.database.windows.net,1433;Initial Catalog=DBName;Persist Security Info=False;User ID=;Password=;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;",
  "CorsOriginLocalHost": "http://localhost:3000",
  "CorsOriginStaging": "https://randomstuffreactjsappzeropoint4.azurewebsites.net",
  "CorsOriginProduction": "https://randomstuffreactjsappzeropoint4.azurewebsites.net",
  "ConnectionStrings": {
    "SQLiteConnectionString": "Data Source=quotesdatabase.db"
  }
}

Update 1 :

I noticed something odd. The following code, does not raise any errors. Actually, sorry, I wasn't injecting cors in my code yet. This also gives the same null error. So, yes, at this point, definitely not able to read information from appsettings.json

builder.Services.AddCors(cors =>
        {
            cors.AddDefaultPolicy( policy =>
            {
                policy.WithOrigins(builder.Configuration["CorsOriginLocalHost"],
                                    builder.Configuration["CorsOriginStaging"],
                                    builder.Configuration["CorsOriginProduction"]);
    
    
                //policy.WithOrigins("http://localhost:3000",
                //                    "http://localhost:3000"
                //                    );
                policy.AllowAnyMethod();
                policy.AllowAnyHeader();
                policy.AllowCredentials();
            }
                );
        }
    );

Update 2 :

Okay, I found the problem. I had changed the 'Working Directory' while debugging SQLite. So, the code kept looking for appsettings.json in the wrong location. I set to the project folder manually. I used to do this step in .NET 5.0.

Looks like it's not required in .NET 6.0

I went back to project properties and removed the path I had set. made it blank. Now, able to read appsettings.json.

Jay
  • 2,648
  • 4
  • 29
  • 58
  • Can you provide more information on how you are launching the application. Also have a check in published folder if the appsettings file is copied over or not? – Durga Prasad Mar 19 '22 at 11:17
  • 1. the appsettings.json is set to copy to output. 2. I am just launching the usual way. default dot net core project template. I did not modify or anything. – Jay Mar 20 '22 at 04:11
  • Looks like the answer has been accepted. Can you let us know how the issue has been resolved? – Durga Prasad Mar 20 '22 at 07:42
  • 1
    I ended up using a combination of my Update 2 above in my question and the answer posted below as the final solution :) – Jay Mar 20 '22 at 08:07

1 Answers1

1
"ConnectionStrings": {
      "ToDoItemsDatabase": "Server=JOHANDRE\\SQL2017; Database=ToDoItems; User=xxx; Password=xxx;"
  },

as shown in ConnectionString in .NET Core 6

is really different from, this:

"SQLiteConnectionString": "Data Source=quotesdatabase.db"

Try to use this in your appsettings.json:

"ConnectionStrings": {
    "SQLiteConnectionString": "Data Source=quotesdatabase.db"
  }
Luuk
  • 12,245
  • 5
  • 22
  • 33
  • 1
    That made no difference. I don't know what is the issue. Why does Microsoft keep changing things that work with every version is beyond me :( – Jay Mar 19 '22 at 09:21
  • In my test, i got the connection string using `builder.Configuration.GetConnectionString("SQLiteConnectionString")`, so you will need some debugging to see which next step is failing. – Luuk Mar 19 '22 at 09:25
  • Well, I have updated my question with additional details. I am beginning to think, this might be an issue only with SQLite. I am able to get read app settings for other services. I also distinctly remember being to use this code last week with Azure SQL server – Jay Mar 19 '22 at 09:29
  • So, no, it's not just with SQLite. I am not able to read from other fields either. – Jay Mar 19 '22 at 09:58
  • this problem has not much to do with reading setting from `appsettings.json`. You could have checked that by checking the values returned from `builder.Configuration.GetDebugView()` (see: [GetDebugView](https://learn.microsoft.com/en-us/dotnet/api/microsoft.extensions.configuration.configurationrootextensions.getdebugview?view=dotnet-plat-ext-6.0)) – Luuk Mar 19 '22 at 10:23
  • Yes, the appsettings.json values are definitely not getting loaded and not seen in the debug view. I dont understand what I am doing wrong :( – Jay Mar 20 '22 at 04:00