I have a web app on Azure that I am trying to connect to a mysql db hosted on aiven.io. The connection works if I hard code the connection string in appsettings.json. When I enter the exact same connection string in Azure Env Variables and leave the localdb connection string in appsettings the connection doesn’t work. Its my understanding that the Azure Env Variables should overwrite whatever is in appsettings.
Here is my env variable setup
Here is my Program.cs connection string setup
var builder = WebApplication.CreateBuilder(args);
var MySqlConnString = builder.Configuration.GetConnectionString("MySqlConn");
builder.Services.AddDbContext<AppDBContext>(options => options.UseMySql(MySqlConnString,ServerVersion.AutoDetect(MySqlConnString)));
appsettings.json
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"JWT": {
"Issuer": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"Audience": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"SigningKey": "SIGNING_KEY_REMOVED"
},
"ConnectionStrings": {
"MySqlConn": "Server=REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOWt;Database=ends;User=root;Password=PASSORDREMOVE;Port=3306;"
}
}
launchSettings.json
{
"$schema": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"sslPort": 44385
}
},
"profiles": {
"http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "REMOVED_URL-QUESTION_GETTING_FLAGGED_AS_SPAM_BY_STACK_OVERFLOW",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
2
Answers
Your startup class is reading the value from connection strings. I have not tried before to do same as you. Usually, I add all my connection string into "Connection Strings" tab.
Because your App Settings still says that you have the connection string with local DB.
Have you tried to add the connection string in the connection string tab?
Example:
Check as well if your application is using a Key Vault. Might be possible that all your data is overwritten with values from Key Vault.
P.S. It might not be critical for you, but for safety purpose try to avoid sharing your JWT information publicly.
If the variable is in "App settings" tab, you could access by
var MySqlConnString = builder.Configuration["ConnectionStrings_MySqlConn"]
;If the variable is in "Connection strings" tab ,you could access by
var MySqlConnString = builder.Configuration.GetConnectionString("MySql1");