Reputation: 77866
What I am trying to achieve
Hello, I have dotnetcore web Api which is running in Azure App Service server farm. I am trying to read the connection string from app service configuration app settings.
What I have tried so far
Below is how I am trying to read the connection string settings
var configValue = (ConfigurationManager.AppSettings["PNLDBConnectionString"] ?? String.Empty).ToString();
I have added the connection string in app service configuration section as below
As well have added a app settings key as below
But in either way, if I am removing the connection string key from app.config
and deploying; then while trying to run any API endpoint throws below error which essentially means the it's not able to read the said connection string property
Any idea, what I could be missing here? Please suggest.
EDIT:
app.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key ="Encrypted" value="true"/>
<add key ="PNLDBConnectionString" value="connection string value"/>
</appSettings>
</configuration>
Upvotes: 2
Views: 2279
Reputation: 133
In .NET Core 6:
After you've added the ConnectionString in appSettings for local debugging (like in HarshithaVeeramalla-MT's answer), add this in Program.cs:
var configValue = builder.Configuration.GetConnectionString("PNLDBConnectionString")
GetConnectionString
comes from Microsoft.Extensions.Configuration
, which should be always available for ASP.NET Core apps.
Upvotes: 0
Reputation: 77866
Looking at the Kudu portal, under environment variables I could see the settings as
PNLDBConnectionString = xxxxxxxxxx
APPSETTING_PNLDBConnectionString = xxxxxxxxx
Using, Environment.GetEnvironmentVariable("APPSETTING_PNLDBConnectionString")
did the trick actually. Had to modify the code line a bit like
var configValue = ConfigurationManager.AppSettings["PNLDBConnectionString"]
?? Environment.GetEnvironmentVariable("APPSETTING_PNLDBConnectionString")
?? String.Empty;
Upvotes: 1
Reputation: 1733
After Adding New ConnectionString in Azure Portal => Configuration => Application settings Add Connection string in appsettings.json file
"ConnectionStrings": {
"DefaultConnection": "Server=SQLAzure; Database=PNLDB; Trusted_Connection=True; MultipleActiveResultSets=true"
}
In Startup.cs, Setup configuration settings to overwrite the environmental variables with appsettings.json.
public IConfiguration Configuration { get; set; }
public Startup()
{
Configuration = new Configuration()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables();
}
Configure the DB in Startup
public void ConfigureServices(IServiceCollection services)
{
services
.AddEntityFramework()
.AddSqlServer()
.AddDbContext<ProjectContext>(options =>
{
var connString = Configuration.Get("PNLDBConnectionString");
options.UseSqlServer(connString);
});
}
Update
var connection =
System.Configuration.ConfigurationManager.
ConnectionStrings["PNLDBConnectionString”].ConnectionString;
Your assembly also needs a reference to System.Configuration.
Upvotes: 1