IOptions configurations throws random NullReferenceException - AspNetCore 2.2

638 Views Asked by At

I have an WebApi app targeting net framework 4.6.1 and using AspNetCore 2.2 :

Setup:

public void ConfigureServices(IServiceCollection services)
{            
    services.Configure<EmailConfig>(Configuration.GetSection("EmailConfig"));

    services.AddTransient<IEmailService, EmailService>();
}

Injecting:

public EmailService(IOptions<EmailConfig> emailConfig)
{
    _emailConfig = emailConfig.Value;
}

The setup works fine at the first look, but under some load testing I have noticed that I get the following error when the DI container tries to get the IOptions<EmailConfig>:

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.Extensions.Configuration.ConfigurationProvider.<>c__DisplayClass9_0.<GetChildKeys>b__0(KeyValuePair`2 kv)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext()
   at System.Linq.Enumerable.<ConcatIterator>d__59`1.MoveNext()
   at System.Linq.Buffer`1..ctor(IEnumerable`1 source)
   at System.Linq.OrderedEnumerable`1.<GetEnumerator>d__1.MoveNext() 
   at System.Linq.Enumerable.<DistinctIterator>d__64`1.MoveNext()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at System.Collections.Generic.List`1.InsertRange(Int32 index, IEnumerable`1 collection)
   at Microsoft.Extensions.Configuration.ChainedConfigurationProvider.GetChildKeys(IEnumerable`1 earlierKeys, String parentPath)
   at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
   at Microsoft.Extensions.Configuration.ConfigurationRoot.GetChildrenImplementation(String path)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.BindInstance(Type type, Object instance, IConfiguration config, BinderOptions options)
   at Microsoft.Extensions.Configuration.ConfigurationBinder.Bind(IConfiguration configuration, Object instance, Action`1 configureOptions)
   at Microsoft.Extensions.Options.ConfigureNamedOptions`1.Configure(String name, TOptions options)
   at Microsoft.Extensions.Options.OptionsFactory`1.Create(String name)
   at System.Lazy`1.CreateValue()

Does anyone got trough this type of problem?

Later Edit: Removing the IOptions<EmailConfig> from the EmailService and injecting the IConfiguration directly and then getting my emailConfigs from IConfiguration manually, works great and solves my problem. But still, in order to respect the ISP and SoC principles when using configurations, the recommended way to do it by the aspnetcore team is using Options Pattern, but looks like it is not working fine all the time.


I am starting to think that maybe there is a problem with my setup: aspnetcore 2.2 targeting netFramework 4.6.1. I'll try to upgrade to the latest netcore and also change the target to netcore to see if the problem reproduces.

0

There are 0 best solutions below