Search code examples
asp.net-core.net-core-rc2

How to manually reload IOptions in ASP.NET Core RC2 application


I have asp.net Core Rc2 application. To store system setting I use IOptions mechanism which is configured like that:

services.AddOptions();
services.AddSingleton<IOptions<DocumentParameters>, DocumentParametersConfigureOptions>();

All parameters are loaded from database in DocumentParametersConfigureOption class. Thanks to this solution, all system settings can be easily injected into controllers/services and are cached on the server side (they are loaded only at the start of application).

I have also a page where settings can be modified (modified in database). I would like to know how can I reload them when user clicks save without restarting web application service.


Solution

  • Use in memory caching.

    On load, put your configuration there. On conclusive connections/resolves, read it from there. When you update it via UI, simply invalidate the cache (i.e. using the cancellation token, see link from above) or just put it manually back in there overriding the existing value.