aspnet / Configuration

[Archived] Interfaces and providers for accessing configuration files. Project moved to https://github.com/aspnet/Extensions

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decoupling Config.Json from Config.FileExtensions

davidkaya opened this issue · comments

Currently if I use JSON configuration, I am able to use it only with configuration that is saved in a file because Config.Json is coupled with Config.FileExtensions through https://github.com/aspnet/Configuration/blob/dev/src/Config.FileExtensions/FileConfigurationSource.cs which contains IFileProvider and https://github.com/aspnet/Configuration/blob/dev/src/Config.FileExtensions/FileConfigurationProvider.cs which also uses IFileProvider for watching the file for changes. If the "source" would be separated from the "format" then we would be able to use Network, File, Command Line etc. with formats as JSON, XML, etc.

If we think about the fact, that JSON is only a format of the configuration where source could be something else, not just file, then currently we would be required to write duplicate code. Even https://github.com/aspnet/Configuration/blob/dev/src/Config.Json/JsonConfigurationFileParser.cs can't be used because it is marked as internal.

public override void Load(Stream stream)
accepts generic Stream however this stream will always be a Stream for a File because of

Use case:

  1. If I have a server which has REST endpoint which provides configuration (e.g. JSON), I would have to write own parser, even though there is already one.
  2. If I have a server where I am connected using e.g. SignalR and the server provides configuration as JSON and also is able to notify me when the configuration changes I should be able to use existing JSON parsing (and other) code and since server notifies me about changes, I would be able to use IChangeToken for notifying my application that the configuration has changed.

Naive Workaround:

builder
    .AddCustomHttpSource(...) // This download JSON configuration from remote server and stores to remote.json
    .AddJsonFile("remote.json");

Yeah we will likely do something about this in 2.2, this is being tracked by https://github.com/aspnet/Configuration/issues/662