5

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,

iliketocode
  • 6,978
  • 5
  • 46
  • 60
SwapnilKumbhar
  • 307
  • 2
  • 5
  • 14
  • Please show us an [MCVE](https://stackoverflow.com/help/mcve). – Steven Apr 22 '19 at 11:14
  • Have you set a breakpoint to confirm that your `Init` method is actually getting called? – Scott Hannen Apr 22 '19 at 12:27
  • 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 support – SwapnilKumbhar Apr 23 '19 at 15:55
  • Does this answer your question? [How to get an instance of IConfiguration in asp.net core?](https://stackoverflow.com/questions/48017390/how-to-get-an-instance-of-iconfiguration-in-asp-net-core) – Ian Kemp Dec 24 '20 at 10:04

2 Answers2

15

I use this one in asp.net core and work 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>();
            }
      }
hassan.ef
  • 1,230
  • 2
  • 11
  • 18
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.

Ricardo Araújo
  • 165
  • 2
  • 7