# Observe settings via provider

**Requires**: [main module](/configuration/modules/configuration.md).

One can subscribe to settings updates for a type with [configuration provider](/configuration/concepts-and-basics/configuration-provider.md)'s **Observe** method.

With prior [assignment of sources](/configuration/basic-scenarios/assign-sources-to-types.md):

```
provider.Observe<MySettings>().Subscribe(newSettings => {});
```

With sources passed on per-call basis:

```
provider.Observe<MySettings>(new JsonFileSource("settings1.json"))
    .Subscribe(newSettings => {});
```

Temporary subscriptions should be disposed of as they might hold on to resources in [sources](/configuration/concepts-and-basics/configuration-sources.md):

```
var subscription = provider.Observe<MySettings>().Subscribe(OnNewSettings);

using (subscription)
{
    // ...
}
```

### Observe method behavior

* `OnError` and `OnCompleted` notifications are never produced. Only successful settings updates are propagated to the observers. This means that there will be no notifications if initial attempt to provide settings [fails with an error](/configuration/concepts-and-basics/error-handling.md) and no further data updates are published by the [source](/configuration/concepts-and-basics/configuration-sources.md).
  * The only way to notice errors when using **Observe** is to [enable error logging](/configuration/basic-scenarios/log-errors.md).

* The subscription is not guaranteed to immediately produce a notification. The first notification may be delayed due to data not having been fetched from the source yet. However, once a valid settings instance has been observed, all new observers receive a notification with current actual settings upon subscription. This notification is published on a background thread, so don't count on it being delivered after `Subscribe` call completes.

  &#x20;

### Best practices

It's recommended to obtain initial settings instance with **Get** method before subscribing to updates with **Observe**. This practice ensures correct error propagation, warms up the [cache](/configuration/concepts-and-basics/caching-and-performance.md) and eliminates situations where the settings are not ready yet on access attempt.&#x20;

```
private volatile MySettings settings;
 
public Task InitializeAsync(IConfigurationProvider provider)
{
    OnSettingsUpdated(settings = provider.Get<MySettings>());
         
    provider.Observe<MySettings>()
      .Subscribe(newSettings => OnSettingsUpdated(settings = newSettings));
 
    return Task.CompletedTask;
}
 
private void OnSettingsUpdated(MySettings settings) 
{
    // ... 
}
```

### Related pages

{% content-ref url="/pages/-M6UQugcsHv63ilSF945" %}
[Configuration provider](/configuration/concepts-and-basics/configuration-provider.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M6UQMklC2EtQ\_nAUDjI" %}
[Configuration sources](/configuration/concepts-and-basics/configuration-sources.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M6URAB0ITAnAJKMQJ6y" %}
[Caching and performance](/configuration/concepts-and-basics/caching-and-performance.md)
{% endcontent-ref %}

{% content-ref url="/pages/-M6UR6uAo\_v35e9-OcDC" %}
[Error handling](/configuration/concepts-and-basics/error-handling.md)
{% endcontent-ref %}


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://vostok.gitbook.io/configuration/basic-scenarios/observe-settings-via-provider.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
