4

I am setting up my first .NET Core application. I am going to user Dapper (1.50.0-rc2) for the ORM.

I have added the following to my appsettings.json file.

"Data": {
    "DefaultConnection": {
        "ConnectionString": "user id=exampleusername;password=examplepassword;Data Source=db.example.com;Database=exampledb;"
    }
},

I am confused at how to get the value of ConnectionString. As .NET Core is so new, online examples are all over the place and none seem to actually cover this.

eat-sleep-code
  • 4,753
  • 13
  • 52
  • 98

2 Answers2

7

My walkthrough:

  1. Add ConnectionStrings section in appsettings.json:

    "ConnectionStrings": {
       "cs1": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
       "cs2": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;",
       "cs3": "Server=xxxx;Port=xxxx;Database=xxxx; User Id=xxxx;Password=xxxx;Pooling=false;"
    },
    
  2. Create class that represents the connection strings section:

    public class ConnectionStringList
    {
        public string cs1 { get; set; }
        public string cs2 { get; set; }
        public string cs3 { get; set; }
    }
    
  3. Open Startup.cs and add the above configuration class to the services collection in ConfigureServices:

    public void ConfigureServices(IServiceCollection services)
    {
        ...
        services.AddOptions();
        services.Configure<ConnectionStringList>(Configuration.GetSection("ConnectionStrings"));
    }
    
  4. Inject IOptions<ConnectionStringList> into your controller/service etc. and retreive your connection string value from Value property:

    public SampleController(IOptions<ConnectionStringList> connectionStrings)
    {
        string cs1 = connectionStrings.Value.cs1;
        ...
    
tarn
  • 352
  • 6
  • 6
5

I have a sample Console App for .NET core on my GitHub repository

Setup phase

var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

Build phase

Configuration = builder.Build();

Use phase

Configuration.GetConnectionString("DefaultConnection")

You can use this value for Dapper

P.S.

You need to add 3 dependencies into your project.json

"Microsoft.Extensions.Configuration": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.FileExtensions": "1.0.0-rc2-final",
"Microsoft.Extensions.Configuration.Json": "1.0.0-rc2-final"

UPDATED

Specific solution

make Configuration static property and add private setter

public static IConfigurationRoot Configuration { get; private set; }

and change your extension

namespace GamesCore.Extensions 
{
    public class ScoreExtensions 
    { 
        private static string dataConnectionString = Startup.Configuration.GetConnectionString("DefaultConnection"); 
    } 
}

For .NET Core 2.0 everything is same and only project file is changed so you need to use following packages:

  <ItemGroup>
    <PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.2" />
    <PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="2.0.2" />
    <PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.2" />
  </ItemGroup>
Mike
  • 3,766
  • 3
  • 18
  • 32
  • I have "Setup phase" and "Build phase" in the Startup method of the Startup.cs (they were already there from the Visual Studio template). I added the missing dependencies to my project.json. But on the "Use phase" I get 'The name 'Configuration' does not exist in the current context' I tried adding a using Microsoft.Extensions.Configuration but that results in 'The type or namespace name 'GetConnectionString' does not exist' error. – eat-sleep-code Jun 02 '16 at 17:23
  • Sorry, hit the enter key too quickly. See updated comment above. – eat-sleep-code Jun 02 '16 at 17:26
  • @eat-sleep-code where are you trying to use the `Configuration` ? – Mike Jun 02 '16 at 17:29
  • In an an extension class; ```namespace GamesCore.Extensions { public class ScoreExtensions { private static string dataConnectionString = Configuration.GetConnectionString("DefaultConnection"); } }``` – eat-sleep-code Jun 02 '16 at 17:39
  • How about .net core 2? – niico May 24 '18 at 10:08
  • 1
    @niico added packages for .net core 2 – Mike May 24 '18 at 13:32