How to read connection string in .NET Core?
Asked Answered
S

16

178

I want to read just a connection string from a configuration file and for this add a file with the name "appsettings.json" to my project and add this content on it:

{
"ConnectionStrings": {
  "DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-

 WebApplica71d622;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
    "Logging": {
    "IncludeScopes": false,
    "LogLevel": {
    "Default": "Debug",
    "System": "Information",
    "Microsoft": "Information"
   }
 }
}

On ASP.NET I used this:

 var temp=ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;

Now how can I read "DefaultConnection" in C# and store it on a string variable in .NET Core?

Syllogism answered 22/8, 2016 at 15:26 Comment(2)
learn.microsoft.com/en-us/aspnet/core/fundamentals/…Blasted
:D :D This is hilarious. That issue should be simple enough and all I can see is tons of answers, each forking into separate ways. I thought by 2022~2023 things should have been simplified! I landed here looking for an answer, I take off same as I came in, empty handed :) Thank you Microsoft.Sherri
F
123

You can do this with the GetConnectionString extension-method:

string conString = Microsoft
   .Extensions
   .Configuration
   .ConfigurationExtensions
   .GetConnectionString(this.Configuration, "DefaultConnection");

System.Console.WriteLine(conString);

or with a structured-class for DI:

public class SmtpConfig
{
    public string Server { get; set; }
    public string User { get; set; }
    public string Pass { get; set; }
    public int Port { get; set; }
}

Startup:

public IConfigurationRoot Configuration { get; }


// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // http://developer.telerik.com/featured/new-configuration-model-asp-net-core/
    // services.Configure<SmtpConfig>(Configuration.GetSection("Smtp"));
    Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<SmtpConfig>(services, Configuration.GetSection("Smtp"));

And then in the home-controller:

public class HomeController : Controller
{

    public SmtpConfig SmtpConfig { get; }
    public HomeController(Microsoft.Extensions.Options.IOptions<SmtpConfig> smtpConfig)
    {
        SmtpConfig = smtpConfig.Value;
    } //Action Controller


    public IActionResult Index()
    {
        System.Console.WriteLine(SmtpConfig);
        return View();
    }

with this in appsettings.json:

"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplica71d622;Trusted_Connection=True;MultipleActiveResultSets=true"
},

"Smtp": {
    "Server": "0.0.0.1",
    "User": "[email protected]",
    "Pass": "123456789",
    "Port": "25"
  }
Fania answered 28/11, 2016 at 12:27 Comment(15)
Configure is an extension method. It should be used most commonly like this: services.Configure<SmtpConfig>(Configuration.GetSection("Smtp")); Sure, it's much the same thing, but I think people not aware will just start doing it the "wrong" way, using the uncommented line, so perhaps best to remove the line. ;)Gustave
@James Wilkins: Very valid concerns. However, I actually prefer this notation over using it as extension method - this way I know what is being done where, and can copy-paste from 1 place to another, without getting problems because of missing import namespaces. The only problem is, MS uses namespaces for taxonomy instead of name collision prevention - because of that, the namespaces are too long. Also, if you remove the namspaces and use the extension methods, the same kind of people will start complaining about the code not compiling. Not everyone uses an IDE, so it's better this way.Fania
I find most people don't bother to put the "usings" in their code examples, which is a case for your point, but also why extension methods fail for the other point. ;)Gustave
@James Wilkins: I completely agree. Usually because they have the using in their code, so they don't know it's an extension method, or because they don't know which of all the using clauses would be needed, so they just post without it. It's getting better with VS 2017, but VS2017 runs only on Windows, plus it's slow (IMHO) - which wouldn't be that bad if it didn't block input, or if starting a debbugger wouldn't take an eternity, but unfortunately, in VS it does.Fania
Yes, it seems the better VS gets, the slower it gets, perhaps offsetting much of the the extra goodness somewhat, lol. ;) Though sometimes it always seems to get slow up to the initial release before they speed it up (seems to be a trend now).Gustave
How can you get the connectionString in your model? Is there some shortcut or is the only way trough injection?Browning
@JedatKinports: No, only injection. Even if you'd write a static method, you'd still need the configuration. You could just read a JSON/YAML file manually, though. But that will eliminate overwrites, such as usersecrets or others (e.g. configuration from registry).Fania
I am getting an error: "MyClass does contain a definition for 'Configuration'..."Immunogenic
What is "this.Configuration" referring to in the connection string part? GetConnectionString(this.Configuration, "DefaultConnection")Junna
@MC9000: The IConfiguration property in the startup class (which gets passed in in the constructor). IConfiguration comes from reading the appsettings.json file, where you have a section "ConnectionString".Fania
Amazing. And for ConnectionString, which is the question about, how to do it?Fulford
@Bisneto: Well, you have several options. The simplest and most atrocious is of course, putting IConfiguration into a static variable. Then you can call GetConnectionString on it wherever you like. However, the smart approch is reading the connection string once, and create a factory class and get the connection string within the factory, once, or if the change token of the config file changes. Then register the factory class in ConfigureServices, and then you don't need to get the connection string at all, or your factory class can expose the connection string as property.Fania
@Bisneto: Actually, the question demonstrates a lack of understanding of Dependency Injection. You don't want a connection string. You want a connection-factory registered as a service. Also, IConfiguration can change (it has a change-token), a static string is bad. See also: Microsoft.Extensions.Primitives.ChangeToken.OnChange - and for an example, see github.com/dotnet/runtime/issues/32735Fania
How about if you want to get the ConnectionString from an appSettings.json file that resides in a UnitTest .net Core project, where you don't have direct access to IConfiguration?Lithographer
2Cristian: Like this (you might want to create a DI service): string json = System.IO.File.ReadAllText("/path/appsettings.json", System.Text.Encoding.UTF8); Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);Fania
M
144

The posted answer is fine but didn't directly answer the same question I had about reading in a connection string. Through much searching I found a slightly simpler way of doing this.

In Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    ...
    // Add the whole configuration object here.
    services.AddSingleton<IConfiguration>(Configuration);
}

In your controller add a field for the configuration and a parameter for it on a constructor

private readonly IConfiguration configuration;

public HomeController(IConfiguration config) 
{
    configuration = config;
}

Now later in your view code you can access it like:

connectionString = configuration.GetConnectionString("DefaultConnection");
Masurium answered 14/7, 2017 at 18:15 Comment(12)
Wouldn't do it like that. If you work without entity framework, you better register a connection factory as singleton, e.g. to use with dapper. You can then still expose a connectionString property if you need to, but I bet it wouldn't be necessary in 99% of cases.Fania
But how to access Configuration in Models instead of Controller?Diagenesis
@Tanmay: You create your Models in such a way that they expect Configuration in the constructor. Then when you create the model in the controller, you pass it Configuration in the constructor. If you call some class in the Controller, which then creates your models, then you need to pass this class the Configuration as well, either in the constructor, or as properties, or as an additional method argument.Fania
The more I read and try things, the more I realize getting a connection string is a major undertaking. I just get nulls no matter what I try.Junna
Yeah. Too many computer scientists creating huge high hanging fruit just to say "Hello World". Unbelievable. Entropy at it's best.Upturn
@JustJohn: I understand your grievance, but proper design is testable, and that means you have to pass dependencies in the constructor, otherwise your application/framework is not unit-testable. This is also proper design, as you can just replace one component with another, without having to change a whole lot of code. If you don't want to pass 100 arguments around, you can also pass the System.IServiceProvider into the class, then you can fetch the dependencies there. But the other side of the coin is that this comes with added complexity.Fania
The Microsoft DI docs say the following - 'Generally, don't directly inject IConfiguration into a controller.` I wouldn't suggest this answer for that reason, and instead suggest using the Options pattern as Stefan has posted.Groundhog
Where come from variable Configuration, passed as parameter to "services.AddSingleton<IConfiguration>" method ?Fulford
@StefanSteiger there's a difference between "proper design" of your own code, though, and something like a connection string which should just be easily available. It's somewhat ridiculous that appsettings.json isn't globally available, that's clearly the entire point of the damn thing.Friarbird
@Jon Story: Oh, should it ? First, confidential information like connection strings should not be accessible in any way where it isn't needed in the first place, for security reasons. Second, if you absolutely want to, you can make it globally available, either by DI, or by just putting it into a static variable/property yourselfs, the framework doesn't do that for you. Third, you should make services that you pass along for data access (e.g. connection factory), instead of passing connection strings. You need the connection string only when you configure the service, at Startup.Fania
I agree with @JustJohn, this is unnecessary over-engineering at its finest. Connection strings should be easily available and shouldn't take several hours to implement. There is no need to make everything abstract and wrapped in factories and god knows what else. Some things should always stay simple.Aerial
@Diagenesis Logically, services should not be injected to models. You can create desired configuration object inside the service that creates your model, and then pass the configuration object to the model constructorMeaningless
F
123

You can do this with the GetConnectionString extension-method:

string conString = Microsoft
   .Extensions
   .Configuration
   .ConfigurationExtensions
   .GetConnectionString(this.Configuration, "DefaultConnection");

System.Console.WriteLine(conString);

or with a structured-class for DI:

public class SmtpConfig
{
    public string Server { get; set; }
    public string User { get; set; }
    public string Pass { get; set; }
    public int Port { get; set; }
}

Startup:

public IConfigurationRoot Configuration { get; }


// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
    // http://developer.telerik.com/featured/new-configuration-model-asp-net-core/
    // services.Configure<SmtpConfig>(Configuration.GetSection("Smtp"));
    Microsoft.Extensions.DependencyInjection.OptionsConfigurationServiceCollectionExtensions.Configure<SmtpConfig>(services, Configuration.GetSection("Smtp"));

And then in the home-controller:

public class HomeController : Controller
{

    public SmtpConfig SmtpConfig { get; }
    public HomeController(Microsoft.Extensions.Options.IOptions<SmtpConfig> smtpConfig)
    {
        SmtpConfig = smtpConfig.Value;
    } //Action Controller


    public IActionResult Index()
    {
        System.Console.WriteLine(SmtpConfig);
        return View();
    }

with this in appsettings.json:

"ConnectionStrings": {
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-WebApplica71d622;Trusted_Connection=True;MultipleActiveResultSets=true"
},

"Smtp": {
    "Server": "0.0.0.1",
    "User": "[email protected]",
    "Pass": "123456789",
    "Port": "25"
  }
Fania answered 28/11, 2016 at 12:27 Comment(15)
Configure is an extension method. It should be used most commonly like this: services.Configure<SmtpConfig>(Configuration.GetSection("Smtp")); Sure, it's much the same thing, but I think people not aware will just start doing it the "wrong" way, using the uncommented line, so perhaps best to remove the line. ;)Gustave
@James Wilkins: Very valid concerns. However, I actually prefer this notation over using it as extension method - this way I know what is being done where, and can copy-paste from 1 place to another, without getting problems because of missing import namespaces. The only problem is, MS uses namespaces for taxonomy instead of name collision prevention - because of that, the namespaces are too long. Also, if you remove the namspaces and use the extension methods, the same kind of people will start complaining about the code not compiling. Not everyone uses an IDE, so it's better this way.Fania
I find most people don't bother to put the "usings" in their code examples, which is a case for your point, but also why extension methods fail for the other point. ;)Gustave
@James Wilkins: I completely agree. Usually because they have the using in their code, so they don't know it's an extension method, or because they don't know which of all the using clauses would be needed, so they just post without it. It's getting better with VS 2017, but VS2017 runs only on Windows, plus it's slow (IMHO) - which wouldn't be that bad if it didn't block input, or if starting a debbugger wouldn't take an eternity, but unfortunately, in VS it does.Fania
Yes, it seems the better VS gets, the slower it gets, perhaps offsetting much of the the extra goodness somewhat, lol. ;) Though sometimes it always seems to get slow up to the initial release before they speed it up (seems to be a trend now).Gustave
How can you get the connectionString in your model? Is there some shortcut or is the only way trough injection?Browning
@JedatKinports: No, only injection. Even if you'd write a static method, you'd still need the configuration. You could just read a JSON/YAML file manually, though. But that will eliminate overwrites, such as usersecrets or others (e.g. configuration from registry).Fania
I am getting an error: "MyClass does contain a definition for 'Configuration'..."Immunogenic
What is "this.Configuration" referring to in the connection string part? GetConnectionString(this.Configuration, "DefaultConnection")Junna
@MC9000: The IConfiguration property in the startup class (which gets passed in in the constructor). IConfiguration comes from reading the appsettings.json file, where you have a section "ConnectionString".Fania
Amazing. And for ConnectionString, which is the question about, how to do it?Fulford
@Bisneto: Well, you have several options. The simplest and most atrocious is of course, putting IConfiguration into a static variable. Then you can call GetConnectionString on it wherever you like. However, the smart approch is reading the connection string once, and create a factory class and get the connection string within the factory, once, or if the change token of the config file changes. Then register the factory class in ConfigureServices, and then you don't need to get the connection string at all, or your factory class can expose the connection string as property.Fania
@Bisneto: Actually, the question demonstrates a lack of understanding of Dependency Injection. You don't want a connection string. You want a connection-factory registered as a service. Also, IConfiguration can change (it has a change-token), a static string is bad. See also: Microsoft.Extensions.Primitives.ChangeToken.OnChange - and for an example, see github.com/dotnet/runtime/issues/32735Fania
How about if you want to get the ConnectionString from an appSettings.json file that resides in a UnitTest .net Core project, where you don't have direct access to IConfiguration?Lithographer
2Cristian: Like this (you might want to create a DI service): string json = System.IO.File.ReadAllText("/path/appsettings.json", System.Text.Encoding.UTF8); Newtonsoft.Json.JsonConvert.DeserializeObject<T>(json);Fania
F
28

In .NET Core 6

appsettings.json

 "ConnectionStrings": {
   "DefaultConnection": "Server=**Server Name**;Database=**DB NAME**;Trusted_Connection=True;MultipleActiveResultSets=true"
  }

Program.cs

var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
builder.Services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(connectionString));

DB Context

public class ApplicationDbContext : DbContext
{
    public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
    {

    }

}
Fallacy answered 5/2, 2022 at 9:31 Comment(2)
Thanks for clarifying, this is so vital - builder.Configuration.GetConnectionString("DefaultConnection"); it is indeed different in .Net 6 sdk.Dosh
I recommend against MARS (multiple active result sets) because this can cause many round-trips to the database, which can be a difficult-to-diagnose performance issue.Prioress
D
27

See link for more info: https://learn.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

JSON

    {
      "ConnectionStrings": {
        "BloggingDatabase": "Server=(localdb)\\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;"
      },
    }

C# Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddDbContext<BloggingContext>(options =>
        options.UseSqlServer(Configuration.GetConnectionString("BloggingDatabase")));
}

EDIT: aspnetcore, starting 3.1: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-3.1

Doorman answered 1/9, 2017 at 0:19 Comment(4)
Why should JSON file should have ConnectionStrings instead of ConnectionString ? Because when I used ConnectionString, then we I'm getting null.Clover
@Clover Try using the prescribed method then ;) Please see the attached link.Doorman
This method seems out of date as of Microsoft.Extensions.Configuration (3.1.5)Literally
@Literally I believe my edit should bring the answer back to being relevantDoorman
S
14

This is how I did it:

I added the connection string at appsettings.json

"ConnectionStrings": {
"conStr": "Server=MYSERVER;Database=MYDB;Trusted_Connection=True;MultipleActiveResultSets=true"},

I created a class called SqlHelper

public class SqlHelper
{
    //this field gets initialized at Startup.cs
    public static string conStr;

    public static SqlConnection GetConnection()
    {
        try
        {
            SqlConnection connection = new SqlConnection(conStr);
            return connection;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }
}

At the Startup.cs I used ConfigurationExtensions.GetConnectionString to get the connection,and I assigned it to SqlHelper.conStr

public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
        SqlHelper.connectionString = ConfigurationExtensions.GetConnectionString(this.Configuration, "conStr");
    }

Now wherever you need the connection string you just call it like this:

SqlHelper.GetConnection();
Stimulus answered 27/11, 2020 at 17:43 Comment(3)
Where is 'connectionString' defined? SqlHelper.connectionString?Robby
@Robby it looks like he renamed it from conStr to connectionString as he mentions both interchangeably.Statued
Neat and clearly explainedHispaniola
D
9

The way that I found to resolve this was to use AddJsonFile in a builder at Startup (which allows it to find the configuration stored in the appsettings.json file) and then use that to set a private _config variable

public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
            .AddEnvironmentVariables();
        _config = builder.Build();
    }

And then I could set the configuration string as follows:

var connectionString = _config.GetConnectionString("DbContextSettings:ConnectionString"); 

This is on dotnet core 1.1

Daves answered 28/1, 2017 at 12:0 Comment(2)
how can i access _config in my control?Pediatrics
By adding it to the DI container in ConfigureServices in Startup.cs.Fania
U
8

ASP.NET Core (in my case 3.1) provides us with Constructor injections into Controllers, so you may simply add following constructor:

[Route("api/[controller]")]
[ApiController]
public class TestController : ControllerBase
{
    private readonly IConfiguration m_config;

    public TestController(IConfiguration config)
    {
        m_config = config;
    }

    [HttpGet]
    public string Get()
    {
        //you can get connection string as follows
        string connectionString = m_config.GetConnectionString("Default")
    }
}

Here what appsettings.json may look like:

{
    "ConnectionStrings": {
        "Default": "YOUR_CONNECTION_STRING"
        }
}
Utility answered 17/4, 2020 at 21:57 Comment(0)
B
4

The method below will work fine if you want to get a connectionString from appsettings.json into a Model or ViewModel (not Controller). This is for ASP.NET Core 3 and above. Sometimes you may need to get a connectionString into a Model (for SQL queries) rather than dependency injection via the controller so this method below will get your connectionString from appsettings:

public class NameOfYourModel
{
  static class getConnection
  {
    public static IConfigurationRoot Configuration;
    public static string GetConnectionString()
    {
      var builder = new ConfigurationBuilder()
      .SetBasePath(System.IO.Directory.GetCurrentDirectory())
      .AddJsonFile("appsettings.json");
       Configuration = builder.Build();
       var connectionString =   
    Configuration.GetConnectionString("connectionStringName");
       
       return connectionString;
     }
    }
    string connStr = getConnection.GetConnectionString().ToString(); //This   
   //line now has your connectionString which you can use. 

 //Continue the rest of your code here.
}
Bainbridge answered 12/2, 2022 at 23:53 Comment(0)
L
3

In 3.1 there is a section already defined for "ConnectionStrings"

System.Configuration.ConnnectionStringSettings

Define:

  "ConnectionStrings": {
    "ConnectionString": "..."
  }

Register:

public void ConfigureServices(IServiceCollection services)
{
     services.Configure<ConnectionStringSettings>(Configuration.GetSection("ConnectionStrings"));
}

Inject:

public class ObjectModelContext : DbContext, IObjectModelContext
{

     private readonly ConnectionStringSettings ConnectionStringSettings;

    ...

     public ObjectModelContext(DbContextOptions<ObjectModelContext> options, IOptions<ConnectionStringSettings> setting) : base(options)
    {
          ConnectionStringSettings = setting.Value;
    }

    ...
}

Use:

   public static void ConfigureContext(DbContextOptionsBuilder optionsBuilder, ConnectionStringSettings connectionStringSettings)
    {
        if (optionsBuilder.IsConfigured == false)
        {
            optionsBuilder.UseLazyLoadingProxies()
                          .UseSqlServer(connectionStringSettings.ConnectionString);
        }
    }
Lacedaemon answered 24/8, 2020 at 17:10 Comment(0)
D
2

There is another approach. In my example you see some business logic in repository class that I use with dependency injection in ASP .NET MVC Core 3.1.

And here I want to get connectiongString for that business logic because probably another repository will have access to another database at all.

This pattern allows you in the same business logic repository have access to different databases.

C#

public interface IStatsRepository
{
            IEnumerable<FederalDistrict> FederalDistricts();
}

class StatsRepository : IStatsRepository
{
   private readonly DbContextOptionsBuilder<EFCoreTestContext>
                optionsBuilder = new DbContextOptionsBuilder<EFCoreTestContext>();
   private readonly IConfigurationRoot configurationRoot;

   public StatsRepository()
   {
       IConfigurationBuilder configurationBuilder = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory)
           .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
       configurationRoot = configurationBuilder.Build();
   }

   public IEnumerable<FederalDistrict> FederalDistricts()
   {
        var conn = configurationRoot.GetConnectionString("EFCoreTestContext");
        optionsBuilder.UseSqlServer(conn);

        using (var ctx = new EFCoreTestContext(optionsBuilder.Options))
        { 
            return ctx.FederalDistricts.Include(x => x.FederalSubjects).ToList();
        }
    }
}

appsettings.json

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft": "Warning",
      "Microsoft.Hosting.Lifetime": "Information"
    }
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "EFCoreTestContext": "Data Source=DESKTOP-GNJKL2V\\MSSQLSERVER2014;Database=Test;Trusted_Connection=True;MultipleActiveResultSets=true"
  }
}
Dallis answered 31/1, 2020 at 14:23 Comment(0)
I
2

This answer is similar to other but a little more straight forward.
Tested in .NET Core 6.
Assuming connection string in appsettings.json like this.

"ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\\xxx"
    "Connect_2": "Data Source=((SERVICE_NAMEXXX"
}

var builder = new ConfigurationBuilder();

builder.AddJsonFile("appsettings.json");

IConfiguration configuration = builder.Build();
string _connstr = configuration.GetConnectionString("DefaultConnection");
string _connstr2 = configuration.GetConnectionString("Connect_2");`

_connstr will hold "Server=(localdb)\xxx".
_connstr2 will hold "Data Source=((SERVICE_NAMEXXX".

Illiterate answered 13/4, 2023 at 23:32 Comment(1)
I think this is the simple and straightforward way for reading a config in .Net Core. Lot of various options are listed and explained at code-maze.com/dotnet-read-connection-strings including usage of ConfigurationBuilder() classCouch
M
1

Too late, but after reading all helpful answers and comments, I ended up using Microsoft.Extensions.Configuration.Binder extension package and play a little around to get rid of hardcoded configuration keys.

My solution:

IConfigSection.cs

public interface IConfigSection
{
}

ConfigurationExtensions.cs

public static class ConfigurationExtensions
{
    public static TConfigSection GetConfigSection<TConfigSection>(this IConfiguration configuration) where TConfigSection : IConfigSection, new()
    {
        var instance = new TConfigSection();
        var typeName = typeof(TConfigSection).Name;
        configuration.GetSection(typeName).Bind(instance);

        return instance;
    }
}

appsettings.json

{
   "AppConfigSection": {
      "IsLocal": true
   },
   "ConnectionStringsConfigSection": {
      "ServerConnectionString":"Server=.;Database=MyDb;Trusted_Connection=True;",
      "LocalConnectionString":"Data Source=MyDb.db",
   },
}

To access a strongly typed config, you just need to create a class for that, which implements IConfigSection interface(Note: class names and field names should exactly match section in appsettings.json)

AppConfigSection.cs

public class AppConfigSection: IConfigSection
{
    public bool IsLocal { get; set; }
}

ConnectionStringsConfigSection.cs

public class ConnectionStringsConfigSection : IConfigSection
{
    public string ServerConnectionString { get; set; }
    public string LocalConnectionString { get; set; }

    public ConnectionStringsConfigSection()
    {
        // set default values to avoid null reference if
        // section is not present in appsettings.json
        ServerConnectionString = string.Empty;
        LocalConnectionString = string.Empty;
    }
}

And finally, a usage example:

Startup.cs

public class Startup
{
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        // some stuff

        var app = Configuration.GetConfigSection<AppConfigSection>();
        var connectionStrings = Configuration.GetConfigSection<ConnectionStringsConfigSection>();

        services.AddDbContext<AppDbContext>(options =>
        {
            if (app.IsLocal)
            {
                options.UseSqlite(connectionStrings.LocalConnectionString);
            }
            else
            {
                options.UseSqlServer(connectionStrings.ServerConnectionString);
            }
        });

        // other stuff
    }
}

To make it neat, you can move above code into an extension method.

That's it, no hardcoded configuration keys.

Meaningless answered 29/10, 2020 at 21:28 Comment(0)
J
1
private readonly IConfiguration configuration;
        public DepartmentController(IConfiguration _configuration)
        {
            configuration = _configuration;
        }

        [HttpGet]
        public JsonResult Get()
        {
string sqlDataSource = configuration["ConnectionStrings:DefaultConnection"];
Joaniejoann answered 17/5, 2021 at 19:22 Comment(0)
C
0

A more handy explanation would be like below:

AppSettings.json:

"ConnectionStrings": {    
"MySQLConnection": "server=localhost; port=3306; database=xxxxx; user=root; password=******; Persist Security Info=False; Connect Timeout=300"
},

And in StartUp:

public class Startup {
    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
    ........
    string mySqlConnection = Configuration.GetConnectionString("NameSetByYouInAppSettings");
Credence answered 1/4 at 7:32 Comment(0)
C
-1

i have a data access library which works with both .net core and .net framework.

the trick was in .net core projects to keep the connection strings in a xml file named "app.config" (also for web projects), and mark it as 'copy to output directory',

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <connectionStrings>
    <add name="conn1" connectionString="...." providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

ConfigurationManager.ConnectionStrings - will read the connection string.

    var conn1 = ConfigurationManager.ConnectionStrings["conn1"].ConnectionString;
Calceolaria answered 6/6, 2019 at 21:7 Comment(1)
If you're using .NET Core, it's best to adopt its configuration pattern instead of shoehorning in the .NET Framework pattern.Kobayashi
D
-3

You can use configuration extension method : getConnectionString ("DefaultConnection")

https://docs.asp.net/projects/api/en/latest/autoapi/Microsoft/Extensions/Configuration/ConfigurationExtensions/index.html#Microsoft.Extensions.Configuration.ConfigurationExtensions.GetConnectionString

Dissection answered 24/8, 2016 at 14:10 Comment(1)
The link appears to be deadInterrelation

© 2022 - 2024 — McMap. All rights reserved.