Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

222
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda