Home > database >  ASP.NET Core app does not use setting from the appsettings.json, after trying to publish to Azure
ASP.NET Core app does not use setting from the appsettings.json, after trying to publish to Azure

Time:02-11

My app when executed locally does not read ConnectionString from the appsettings.json. I had configured "Connected Services" in VS2019 for my ASP.NET Core 3.1 app. Where the SQL DB connection got a wrong DB name (probably got autogenerated). I then have deleted this configuration and ignored it from my profile. But now when I'm trying to execute this app locally - it is still trying to use DB from that config.

Microsoft.Data.SqlClient.SqlException (0x80131904): Cannot open database "aspnet-53bc9b9d-9d6a-45d4-1122-2a2761773502" requested by the login. The login failed.

1. Is there a place it is got cached at or where I need to remove it manually from?

2. Also why it does not read from my appsettings.json?

appsettings.json:

**{
  "Serilog": {
    "Using": [
      "Serilog.Sinks.Console",
      "Serilog.Sinks.File",
      "Serilog.Sinks.Seq",
      "Serilog.Sinks.Loggly"
    ],
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Microsoft": "Debug",
        "System": "Debug"
      }
    },
    "Enrich": [
      "FromLogContext",
      "WithMachineName",
      "WithProcessId",
      "WithThreadId"
    ],
    "WriteTo": [
      {
        "Name": "Console"
      },
      {
        "Name": "File",
        "Args": {
          "path": "C:\\ODATA\\Logs\\log.json"
        },
        "formatter": "Serilog.Formatting.Json.JsonFormatter, Serilog"
      }
    ],
    "Properties": {
      "Application": "ODataTestApp"
    }
  },
  "ConnectionStrings": {
    "Database": "Data Source=*MyDBConnectionString*"
  },
  "AllowedHosts": "*",
  "ApplicationInsights": {
    "ConnectionString": "InstrumentationKey=*my-key*/"
  }
}**

P.S. I added those Connected Services as part of the Publish to Azure steps. I have tried to Publish my app to Azure, but it failed (for different reasons).

CodePudding user response:

It should be appsettings.json.

CodePudding user response:

In local when we debug the application, it will read the configuration from appsettings.dev.json file.

Automatically set appsettings.json for dev and release environments in asp.net core?


When we deploy the application to azure. By default, it will read the appsettings.json file.

But if we have configure the connection string in azure portal, it will overwrite the connection string.

The settings in the azure portal take precedence over the configuration in the appsettings.json file in the project file.

CodePudding user response:

By running locally I am taking it that you mean in debug mode from Visual Studio. Doing the following two things should fix the issue:

  1. Search the solution for "aspnet-53bc9b9d-9d6a-45d4-1122-2a2761773502" and correct anywhere that is set (settings files or otherwise).

  2. Delete the debug and obj folders in the project directory. Clean and rebuild the solution.

The other possibility is that there is an environment variable set on your machine for the connection string, but it seems unlikely that this would be there without you already being aware.

  • Related