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

前端 未结 2 663
我寻月下人不归
我寻月下人不归 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:06

    @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;

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题