3

I need to get values from appsettings.json file in a .Net Core (2.0) class library. There are lots of solutions for ASP.Net but not for .Net Core. My appsettings.json like below;

{
   {

    "serviceAccountEmail": "******@dotnet****.iam.gserviceaccount.com",
    "Certificate": "*********.p12",
    "secret": "*********",
    "ImpersonatedUser": "*********@*********.com",
    "GoogleApplicationName": "********"
    }
}
AshleyCam
  • 59
  • 1
  • 7

1 Answers1

3

You need to set that up in your Startup configuration

public class Startup
{
    private readonly IConfiguration _config;

    public Startup(IConfiguration config)
    {
        _config = config;
    }

    public void ConfigureServices(IServiceCollection services)
    {
        var value = _config["key"];
    }

    public void Configure(IApplicationBuilder app, IConfiguration config)
    {
        var value = config["key"];
    }
}

MS Docs: https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-2.2#access-configuration-during-startup

Separation of Concerns

If you separate your solution into multiple projects with use of class libraries, Microsoft.Extensions.Options.ConfigurationExtensions package comes in handy for reading the values from appsettings files and injecting them into your configuration classes within projects.

It has 2 extensions you can use:

public static T Get<T>(this IConfiguration configuration);
public static IServiceCollection Configure<TOptions>(this IServiceCollection services, 
    IConfiguration config) where TOptions : class;

Example:

I put all security related services using Microsoft.AspNetCore.Identity into its own project called DL.SO.Services.Security.

Security settings in appsettings.json

I define configurations, called "AppIdentitySettings", for identity options I want to set up in ASP.NET Core Identity framework in my web/start-up project.

{
    "ConnectionStrings": {
        ...
    },
    "AppIdentitySettings": {
        "User": {
            "RequireUniqueEmail": true
        },
        "Password": {
            "RequiredLength": 6,
            "RequireLowercase": true,
            "RequireUppercase": true,
            "RequireDigit": true,
            "RequireNonAlphanumeric": true
        },
        "Lockout": {
            "AllowedForNewUsers": true,
            "DefaultLockoutTimeSpanInMins": 30,
            "MaxFailedAccessAttempts": 5
        }
    },
    "Recaptcha": {
        ...
    },
    ...
}

Configuration classes

Then you need to define configuration classes, which are just POCOs, to represent your configuration structure in appsettings.json. The name of the configuration class doesn't have to match the section name you define in the appsettings.json, but rather the names of the properties need to match.

namespace DL.SO.Services.Security
{
    public class AppIdentitySettings
    {
        public UserSettings User { get; set; }
        public PasswordSettings Password { get; set; }
        public LockoutSettings Lockout { get; set; }
    }

    public class UserSettings
    {
        public bool RequireUniqueEmail { get; set; }
    }

    public class PasswordSettings
    {
        public int RequiredLength { get; set; }
        public bool RequireLowercase { get; set; }
        public bool RequireUppercase { get; set; }
        public bool RequireDigit { get; set; }
        public bool RequireNonAlphanumeric { get; set; }
    }

    public class LockoutSettings
    {
        public bool AllowedForNewUsers { get; set; }
        public int DefaultLockoutTimeSpanInMins { get; set; }
        public int MaxFailedAccessAttempts { get; set; }
    }
}

Above is from and in more detail: https://stackoverflow.com/a/46940811/2343826

Fraze
  • 908
  • 2
  • 8
  • 20
  • 1
    In this solution, values are called from a main method, in my case the values should be read from a class library- means there is no main method. – AshleyCam Feb 04 '19 at 17:49
  • @AshleyCam revised in the answer. – Fraze Feb 04 '19 at 17:56
  • Hi, In my scenario, I have different project in on solution and my appsetting.json is in MVC project of the solution and rest at class library project. In this can how can I access the configuration setting using DI for the same appsetting.json throughout the project. MVC code access my class library by creating object like ClassName obj= new ClassName(). Here I get compile time error for all the obvious reason as I define a parameterized constructor for the class to access Configuration setting. Is there any way I can achieve this as have tried lots of solutions – Aditya Pewekar Jan 13 '20 at 18:28