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,

John Smith
  • 7,243
  • 6
  • 49
  • 61
SwapnilKumbhar
  • 347
  • 2
  • 5
  • 17
  • 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
  • Check this out for .NET 6: https://stackoverflow.com/q/76909010/8644294 – Ash K Aug 16 '23 at 14:49

2 Answers2

21

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>();
    }
}
zcoop98
  • 2,590
  • 1
  • 18
  • 31
hassan.ef
  • 1,300
  • 2
  • 11
  • 19
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
  • 191
  • 1
  • 2
  • 10