Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

220
Views
How does .net refresh the value from json when using Options pattern?

I am working on a web api project.

I have some key values stored in the app settings json file.

I have configured the Options patterns so that the settings from json are mapped to an object. I am reading that the options pattern is having singleton scope by default. This means that, at the first request of a controller, the injected option will get initialized with values from the json.

Now when I change a value in the json file, then do I need to restart the application to ensure that the subsequent request will have the latest setting value? Or is there way to make the Option to be scoped so that at each request it will fetch latest value from the settings?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

This is explained in the docs, in Options pattern in ASP.NET Core. How you read the configuration settings depends on the interface you use. This is described in the section with the unintuitive title Options interfaces. I doubt anyone would think to go to that section if one didn't already know about eg IOptionsMonitor.

As that section explains:

  • IOptions<> is indeed a singleton, loads the values at startup and doesn't show any changes.
  • IOptionsMonitor will raise notifications every time there's a change and allow you to retrieve the latest value. This means that you can easily read two different values for the same setting in the same action.

Imagine trying to calculate a price while a fee or commission setting changes. This can be annoying.

  • IOptionsSnapshot<> is scoped, will load the settings when the scope starts and won't changed during the scope, even if the underlying settings change. If your Options object has a Fee property, that will stay the same even if the actual setting changes.

You specify how you want to retrieve the values by using the appropriate interface in your controller, BackgroundService or other DI service class, eg :

class MyController:ControllerBase
{

    IOptionsSnapshot<Fees> _fees;

    public MyController(IOptionsSnapshot<Fees> feeOptions)
    {
        _fees=feeOptions'
    }
    ...
}

The section Use IOptionsSnapshot to read updated data offers more examples

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!