How to include reference to assembly in ASP.NET Core project

DmitryBoyko picture DmitryBoyko · Nov 23, 2016 · Viewed 20.1k times · Source

I have this line

string sConnectionString = ConfigurationManager.ConnectionStrings["Hangfire"].ConnectionString;

And it requires to include System.Configuration

In which place of the project I have to add reference to System.Configuration because I cannot find a classic place to do it under References?

enter image description here

Answer

haim770 picture haim770 · Nov 23, 2016

The tutorial your're following is probably using Asp.Net Core targeting the full .Net Framework (4.6) that is capable of relying on System.Configuration (that is not portable and not supported in CoreFX).

.Net Core projects (being cross-platform) use a different configuration model that is based on Microsoft.Extensions.Configuration rather than on System.Configuration.

Assuming your Hangfire connection-string is defined in your appsettings.json:

{
     "ConnectionStrings": {
         "HangFire": "yourConnectionStringHere"
     }
}

You can read it in your Startup.cs:

public class Startup
{
    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)

        this.Configuration = builder.Build();

        var hangFireCS = this.Configuration.GetConnectionString("HangFire");
    }
}

Also, you're gonna need the Microsoft.Extensions.Configuration.Json package to use the AddJsonFile() extension method.