You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Improve the performance of configuration binding #36130
Is your feature request related to a problem? Please describe.
I have a configuration class which contains over 50 properties. I want to get the new configuration for each request so IOptionsSnapshot is my choice, but the problem is that configuration is using Reflection to bind the options for each request. And for obvious reasons it can't be cached like IOptions or IOptionsMonitor.
Also breaking the configuration class to multiple class is not an option here.
Describe the solution you'd like
Is it good idea to create a method using ILGenerator at runtime and use that to bind the configuration class? I assume once the method is created it has better performance than reflection.
P.S. I'm willing to create a PR for this.
Reacted by Shayne van Asperen, Alexey Malinin, Giorgi Chkhikvadze, cr, Teneko, Maximilian Haru Raditya, Juliano Leal Goncalves and Pedro Pablo Rodriguez Carranza
I think we need a new API here since we're going to end up wanting to cache these delegates. We can cache internally but then we need to decide how to properly evict items from the cache (like Activator.CreateInstance) or we can provide an API that returns the bind delegate (I'd prefer that).
Proposed API
namespace Microsoft.Extensions.Configuration
{
public class ConfigurationBinder
{
+ public static Action<IConfiguration, T> CreateBinder<T>(Action<BinderOptions> configureOptions = null);
}
Activity
Kahbazi commentedon May 8, 2020
This could improve the binding performance.
@davidfowl Can we use the new source generators to bind configs?
czd890 commentedon Sep 10, 2020
Any update?
davidfowl commentedon Oct 28, 2020
I think we need a new API here since we're going to end up wanting to cache these delegates. We can cache internally but then we need to decide how to properly evict items from the cache (like Activator.CreateInstance) or we can provide an API that returns the bind delegate (I'd prefer that).
Proposed API
namespace Microsoft.Extensions.Configuration { public class ConfigurationBinder { + public static Action<IConfiguration, T> CreateBinder<T>(Action<BinderOptions> configureOptions = null); }Usage Examples
This API would allow the calling code to appropriately cache.
Alternative Designs
We could change the
Action<IConfiguration, T>toFunc<IConfiguration, T>and it would return the object instance.[-]Using ILGenerator to bind configuration[/-][+]Improve the performance of configuration binding[/+]rosebyte commentedon Mar 2, 2026
Triage: using source generated binder should resolve the performance penalty from using reflection now.