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

前端 未结 2 652
我寻月下人不归
我寻月下人不归 2021-02-19 09:50

I have this line

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

And it requires to include

2条回答
  •  既然无缘
    2021-02-19 10:21

    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.

提交回复
热议问题