IOptionsMonitor<T>.OnChange is fired whenever anything changes in IConfiguration - pit of failure #109445
Labels
area-Extensions-Options
enhancement
Product code improvement that does NOT require public API changes/additions
Milestone
IOptionsMonitor<T>
is the .NET abstraction to handle dynamically changing configuration. That means changes to config file on filesystem during development but also potentially frequent changes through a dynamic config service s.a. Microsoft's internal ones or others.It provides an option to register callback
IOptionsMonitor<T>.OnChange(...)
which is fired when the configuration that's bound to T changes.One would assume that the callback is only called when the config section specific to the
IOptionsMonitor
changes. As the docs don't say otherwise.That is, however, not true (see github issue). The .OnChange callbacks can be invoked even if an unrelated section for some other T changes. Essentially as long as any part of configuration changes, all
IOptionsMonitorcan
trigger their.OnChange
callbacks.It wouldn't be a problem if everyone handled this correctly and actually did rebuilding only if the config actually changes. But regrettably that's not happening.
I think it's not happening because it's almost impossible to not fall into this pit of failure as nothing in the API suggests it would be necessary. And furthermore, doing the right thing is surprisingly, if not difficult, then cumbersome. OnChange for some reason doesn't surface last version of config, only current; so anyone wanting to properly guard against non-changes needs to save the old version themselves, ... .
It's not a huuuuge problem because usually it's just recreating stuff. In case of polly a stateful policies there might be some impact but genenerally it should be functionally ok. The issue is that if all components start rebuilding with each update, service owners will see spikes that don't make much sense.
What to do with it (some random ideas):
The text was updated successfully, but these errors were encountered: