I have hosted Azure Function V3 on Azure Linux environment. I am trying to read the connection string from the configuration section. But I am not getting it. I tried to put the connection string on both, Application Settings
as well as Connection Strings
sections as shown below.
I am using dependency injection and my Startup
class looks like below.
using BHD.Data.Data;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using System;
[assembly: FunctionsStartup(typeof(BHD.AzureFunctions.Startup))]
namespace BHD.AzureFunctions
{
class Startup : FunctionsStartup
{
public override void Configure(IFunctionsHostBuilder builder)
{
var sqlConnection = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
builder.Services.AddDbContext<ApplicationDbContext>(
options => options.UseSqlServer(sqlConnection));
}
}
}
I get NullPointerException
on ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString
even though the connection string exists in local.settings.json
file as shown below.
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true"
},
"ConnectionStrings": {
"DefaultConnection": "<My connection string>"
}
}