简体   繁体   中英

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

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 "as.net-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*/"
  }
}**

PS 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).

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 "as.net-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.

It should be appsetting s .json.

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.

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