Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access App key data from class libraries in .NET Core / ASP.NET Core

To access App Keys in a class library, do we need to do the following code in every class library and class where we need to access a AppKey?

public static IConfigurationRoot Configuration = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

This is what I found in Microsoft docs, but this looks very redundant.

Startup class in a project as below

 public class Startup
    {
        public IConfigurationRoot Configuration { get; set; }

        public Startup()
        {
            var builder = new ConfigurationBuilder()
                .AddJsonFile("appsettings.json");

            Configuration = builder.Build();
        }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddEntityFramework().AddEntityFrameworkSqlServer()
                .AddDbContext<DbContext>(options =>
                    options.UseSqlServer(Configuration["Data:MyDb:ConnectionString"]));
        }
    }

Then how should I inject this "IConfigurationRoot" in each class of a project. And do I have to repeat this Startup class in each class Library? Why is this not part of .NET Core Framework?

like image 218
HaBo Avatar asked Jul 25 '16 15:07

HaBo


People also ask

How can I get the connection string from Appsettings json in NET Core in class library?

var configuration = new Configuration(); configuration. AddJsonFile("appsetting. json"); var connectionString= configuration. Get("connectionString");

Can class library have app config?

Class libraries can access configuration settings in the same way as executable apps, however, the configuration settings must exist in the client app's App. config file.

Can we use NET Framework class library in .NET Core?

If you want to use full . NET Framework libraries, you can only target . NET Framework, unless you use multi-targeting and provide alternate implementations of those libraries for the . Net Core targets.


1 Answers

The recommended way is to use the options pattern, provided by Microsoft and used heavily in ASP.NET Core.

Basically you create a strong typed class and configure it in the Startup.cs class.

public class MySettings 
{
    public string Value1 { get; set; }
    public string Value2 { get; set; }
}

and initialize it in the Startup class.

// load it directly from the appsettings.json "mysettings" section
services.Configure<MySettings>(Configuration.GetSection("mysettings"));

// do it manually
services.Configure<MySettings>(new MySettings 
{
    Value1 = "Some Value",
    Value2 = Configuration["somevalue:from:appsettings"]
});

then inject these options everywhere you need it.

public class MyService : IMyService
{
    private readonly MySettings settings;

    public MyService(IOptions<MySettings> mysettings) 
    {
        this.settings = mySettings.Value;
    }
}
like image 151
Tseng Avatar answered Oct 15 '22 13:10

Tseng