How to include reference to assembly in ASP.NET Core project
Asked Answered
W

2

15

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

Walking answered 23/11, 2016 at 12:17 Comment(2)
.Net Core projects use a different configuration model that is based on Microsoft.Extensions.Configuration rather than System.Configuration. Where is your actual HangFire connection-string is set? In your appsettings.json?Teodor
@Teodor I am study this manual talkingdotnet.com/integrate-hangfire-with-asp-net-core-web-apiWalking
T
11

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.

Teodor answered 23/11, 2016 at 13:6 Comment(2)
I agree with you but why the manual I am facing has presented the old approach that is not supported now in Asp.Net Core ? Does it mean that manual is incorrect?Walking
There's nothing "incorrect" about it, per se. You can build Asp.Net Core apps that are targeting the full framework (4.6). But you won't gain all the benefits and portability of the Core framework.Teodor
A
-2

@Dimi Right Click on References folder -> Add References -> Search configuration -> check on System.Configuration -> Click on ok.

then add Namespaces on your .cs file i.e

using System.Configuration;

enter image description here enter image description here

Acquainted answered 23/11, 2016 at 12:27 Comment(7)
It says NO ITEMS FOUNDWalking
Sorry I cannot see the same windows like you. I have created ASP.NET Core Webapi project. See my updated question, pleaseWalking
learn.microsoft.com/en-us/aspnet/core/fundamentals/…Acquainted
visit above linksAcquainted
Sorry but it does not help to execute a simple operation to add an assembly that we did in Visual Studio during last 12 years.Walking
@Dimi: This is ASP.NET Core/.NET Core it is completely modular and based on NuGet packages. You won't have the files lying around as with .NET Framework before. Whatever you need, you need to reference it in project.json (soon inside csproj when VS2017 comes)Firecrest
@Firecrest Hi! Please provide the detailed answer and I will vote it.Walking

© 2022 - 2024 — McMap. All rights reserved.