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,
Init
method is actually getting called? – Lucid