Why doesn't IConfigurationProvider have an async Load method?
This might be a small or silly thing to ask about/for, but why isn't IConfigurationProvider.Load() async, returning a Task? Most configuration loading is going to be async, for example getting something from the filesystem or from the network. Now implementors are forced to use GetAwaiter().GetResult() in this method.
Honestly, it was probably just an oversight. You are correct that many providers do I/O. Most providers currently do this by doing the I/O synchronously.
I'll put this in the backlog, but it's a fairly high-cost change since it would be extremely viral. It would mean adding ConfigurationBuilder.BuildAsync, which would again propagate up to the callers.
I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.
@mariusGundersen thanks for the issue. The best next step on this would be to prepare the API proposal, usage, etc. in the issue page here (following https://github.com/dotnet/runtime/blob/master/docs/project/api-review-process.md).
Hi all, any progress on that? We are working on our async application improvements and it would be super cool to have it async as well :)
@DkozhevnikovConfigit do you have some usage examples in mind to help shape the API?
@maryamariyan it's really simple example. We are storing our configuration within the DB, so when we want to load it in async manner we call GetAwaiter().GerResult() in Load and TryGet methods.
public class OurConfigurationProvider : ConfigurationProvider
{
public override void Load()
{
Data = _db.GetConfigurationsAsync().GetAwaiter().GerResult();
}
public override bool TryGet(string key, out string value)
{
if (!Data.Contains(key))
{
var configuration = _db.GetConfigurationAsync(key).GetAwaiter().GerResult();
if (configuration != null)
{
Data[key] = configuration;
}
}
value = Data[key];
return true;
}
}
so it would be cool to do it async like:
public class OurConfigurationProvider : ConfigurationProvider
{
public override async Task LoadAsync()
{
Data = await _db.GetConfigurationsAsync();
}
public override async Task<string> GetAsync(string key)
{
if (!Data.ContainsKey(key))
{
var configuration = await _db.GetConfigurationAsync(key);
if (configuration != null)
{
Data[key] = configuration;
}
else
{
return null;
}
}
return Data[key];
}
}
Hi, any update on this?
We are missing this option as well. We're loading configuraition from http endpoint and would be really nice if we could use async here.
Thanks
FWIW I just ran into this with internal code at my employer. I also would like to see this have async APIs.
This really needs to be added. I'm loading a large configuration from a database, its not nice having to force other async API's to run synchronously because the .NET has gaps where it doesn't support it.
I've just encountered this as well - we want to have our appsettings.json use paths to secrets in AWS Secrets Manager - with a custom IConfigurationSource and ConfigurationProvider, we are able to resolve those paths to actual secrets but should be able to do so asynchronously.
I also vote for this.
Yes, i'm waiting for this feature as well
I agree with the comments above and also need that feature to get a configuration with a custom provider over HTTP calls.
At least this ticket has only been open for 3.5 yrs.......
I'm using Vault for getting secrets via http so async implementation is very useful such this scenarios.
I've implemented an async adaptor over here
I'll put this in the backlog, but it's a fairly high-cost change since it would be extremely viral. It would mean adding
ConfigurationBuilder.BuildAsync, which would again propagate up to the callers.
It is possible to use an async provider without the virality, but you have to create an instance of the async provider first and call InitialiseAsync() on it to prefetch the initial values, prior to adding to ConfigurationBuilder. That way when ConfigurationBuilder.Build is called the provider can return its prefetched config synchronously. The other aspect is to write an async version of ChangeToken.OnChange -> this allows the provider to handle change token reloads asynchronously.
I have a similar scenario like @MCKanpolat. An async implementation would be really useful. Plus one for async!
Does anyone know how I could safely fetch config asynchronously from TryGet override?
public override bool TryGet(string key, out string value)
{
if (Data.ContainsKey(key))
{
//can't await?
FetchConfigAsync(key).RunSynchronously();
}
return base.TryGet(key, out value);
}
@Edgaras91 You should probably open a separate Issue or ask your question on a Q&A site like StackOverflow. I'm not really sure what you're trying to do. FetchConfigAsync is already async, so there's no need to wrap it in another async call, and your additional Data.ContainsKey validation isn't actually doing anything as written. All that said, if you just declare TryGet async then you can use await.
@Edgaras91 You should probably open a separate Issue or ask your question on a Q&A site like StackOverflow. I'm not really sure what you're trying to do.
FetchConfigAsyncis already async, so there's no need to wrap it in another async call, and your additionalData.ContainsKeyvalidation isn't actually doing anything as written. All that said, if you just declareTryGetasyncthen you can useawait.
You cant declare the overridden method as async because the return type would need to be Task or Task<T> which wouldn't match the virtual signature.