I can't wrap my head around how to retrieve connection strings in ASP .NET Core. I've spent two days trying all the different things and can't get it to work.
I have tried the following methods:
ConfigurationManager.ConnectionStrings["SupplierDB"].ConnectionString
// NullReferenceException: Object reference not set to an instance of an object.
ConfigurationManager.ConnectionStrings[0].ConnectionString
// data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true
ConfigurationManager.AppSettings.Count.ToString();
// Returns 0
I've got same connection strings specified in both appsettings.json and web.config. Can someone please point me in the right direction as I'm currently completely lost as nothing works.
appsettings.json
{
"ConnectionStrings": {
"CustomerDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False",
"SupplierDB": "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="CustomerDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Customer_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
<add name="SupplierDB" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Supplier_Db;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"/>
</connectionStrings>
</configuration>
You can create an AppSettings
class like this, by copying your appsettings.json to QuickType:
public class AppSettings
{
public static ConnectionStrings ConnectionStrings { get; set; }
public static Logging Logging { get; set; }
public static string AllowedHosts { get; set; }
}
public class ConnectionStrings
{
public string CustomerDb { get; set; }
public string SupplierDb { get; set; }
}
public class Logging
{
public LogLevel LogLevel { get; set; }
}
public class LogLevel
{
public string Default { get; set; }
public string Microsoft { get; set; }
public string MicrosoftHostingLifetime { get; set; }
}
Then in Startup
:
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
Configuration.Get<AppSettings>();
services.AddControllersWithViews();
}
I use static
for easy access instead of injecting AppSettings
through DI.
Or if you don't want to put them all in a class, check out the Options pattern from Microsoft.