Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 489
Friend Avatar asked Nov 23 '16 12:11

Friend


People also ask

How do I add a project reference in Visual Studio NET core?

You can also right-click the project node and select Add > Project Reference. If you see a References node in Solution Explorer, you can use the right-click context menu to choose Add Reference. Or, right-click the project node and select Add > Reference.

How do I add a reference to class library in .NET core?

Add Class Library Reference To use a class library in your application, you must add a reference to the library to access its functionality. Right click on the project name of your console app in Solution Explorer and select Add ->Reference option.

How do I reference an assembly in C#?

In the Project Designer, click the References tab. Click the Add button to open the Add Reference dialog box. In the Add Reference dialog box, select the tab indicating the type of component you want to reference. Select the components you want to reference, and then click OK.


1 Answers

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.

like image 65
haim770 Avatar answered Sep 28 '22 02:09

haim770