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

走远了吗. 提交于 2020-01-01 08:47:11

问题


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?


回答1:


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.




回答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;



来源:https://stackoverflow.com/questions/40764353/how-to-include-reference-to-assembly-in-asp-net-core-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!