Dependency Injection not working for IConfiguration C#.Net Core
Asked Answered
T

2

8

I have injected IConfiguration using following code:

 public class InjectorConfig
    {
        /// <summary>
        /// configuration for DI
        /// </summary>
        /// <param name="services"></param>
        /// <param name="configuration"></param>
        public static void Init(IServiceCollection services, IConfiguration configuration)
        {
            services.AddSingleton<IConfiguration>(provider => configuration);
            services.AddSingleton<AppSettingUtil>();
        }
}                             

while using this in my class called AppSettingUtil I am getting null pointer exception on IConfiguration object.

Below is the code I am using

public class AppSettingUtil
    {     
       public AppSettingUtil(IConfiguration configuration)
       {
          _configuration = configuration;
       }
       public IConfiguration Configuration { get; }
    }

While executing below function I am getting null pointer exception

 private static object GetDefault(string name)
    {
        if (_configuration[name] != null)
        {
            return Convert.ToInt32(_configuration[name]);
        }
        return null;
    }

While executing this function the object _configuration is null, and hence throwing null pointer exception,

Tokay answered 22/4, 2019 at 6:49 Comment(5)
Please show us an MCVE.Einstein
Have you set a breakpoint to confirm that your Init method is actually getting called?Lucid
I got the issue, it was just a silly mistake, Method getDefault() is a static method and I am calling it by using class name(ie. without creating object) hence the constructor is not getting executed and _configuration is not getting initialized, Thanks for the supportTokay
Does this answer your question? How to get an instance of IConfiguration in asp.net core?Arana
Check this out for .NET 6: https://mcmap.net/q/1321751/-use-different-appsettings-json-on-different-machines-having-same-environment/8644294Tizzy
C
23

I use this one in ASP.Net Core and works it for me:

public class Startup
{
    public Startup(IHostingEnvironment env , IConfiguration configuration)
    {
        Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddSingleton<IConfiguration>(provider => configuration);
        services.AddSingleton<AppSettingUtil>();
    }
}
Craggie answered 22/4, 2019 at 7:33 Comment(0)
S
6

It also can be done as follows:

(I did this in the main thread of a .net core console app)

public static void Main(string[] args)
        {

            IConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
            // Duplicate here any configuration sources you use.
            configurationBuilder.AddJsonFile("AppSettings.json");
            IConfiguration configuration = configurationBuilder.Build();

            Program.token = configuration["token"];
            Program.guidID = configuration["guidID"];
            Program.kind = configuration["kind"];

I found the solution in this stackoverflow question.

It works for me. Hope it works for you too.

Swop answered 7/1, 2020 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.