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

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • .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`? – haim770 Nov 23 '16 at 12:57
  • @haim770 I am study this manual http://www.talkingdotnet.com/integrate-hangfire-with-asp-net-core-web-api/ – NoWar Nov 23 '16 at 12:57

2 Answers2

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.

haim770
  • 48,394
  • 7
  • 105
  • 133
  • 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? – NoWar Nov 23 '16 at 13:40
  • 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. – haim770 Nov 23 '16 at 13:44
-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

Mohit Solanki
  • 261
  • 3
  • 17
  • 1
    It says NO ITEMS FOUND – NoWar Nov 23 '16 at 12:29
  • Sorry I cannot see the same windows like you. I have created ASP.NET Core Webapi project. See my updated question, please – NoWar Nov 23 '16 at 12:31
  • 1
    https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration – Mohit Solanki Nov 23 '16 at 12:36
  • 1
    visit above links – Mohit Solanki Nov 23 '16 at 12:36
  • 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. – NoWar Nov 23 '16 at 12:45
  • 1
    @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) – Tseng Nov 23 '16 at 12:53
  • @Tseng Hi! Please provide the detailed answer and I will vote it. – NoWar Nov 23 '16 at 12:55