runtime icon indicating copy to clipboard operation
runtime copied to clipboard

Why doesn't IConfigurationProvider have an async Load method?

Open mariusGundersen opened this issue 7 years ago • 29 comments

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.

mariusGundersen avatar Nov 28 '19 12:11 mariusGundersen

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.

analogrelay avatar Jan 23 '20 18:01 analogrelay

I couldn't figure out the best area label to add to this issue. Please help me learn by adding exactly one area label.

Dotnet-GitSync-Bot avatar May 07 '20 20:05 Dotnet-GitSync-Bot

@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).

maryamariyan avatar Oct 01 '20 22:10 maryamariyan

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 :)

dkogithub avatar Mar 25 '21 12:03 dkogithub

@DkozhevnikovConfigit do you have some usage examples in mind to help shape the API?

maryamariyan avatar Mar 26 '21 04:03 maryamariyan

@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];
      }
    }

dkogithub avatar Mar 26 '21 15:03 dkogithub

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

eduardgrinberg avatar Jul 18 '21 10:07 eduardgrinberg

FWIW I just ran into this with internal code at my employer. I also would like to see this have async APIs.

JasonBock avatar Apr 13 '22 14:04 JasonBock

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.

KieranDevvs avatar Nov 21 '22 11:11 KieranDevvs

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.

piekstra avatar Dec 01 '22 22:12 piekstra

I also vote for this.

dominikjeske avatar Mar 03 '23 10:03 dominikjeske

Yes, i'm waiting for this feature as well

sergey-guk-dev avatar Mar 17 '23 12:03 sergey-guk-dev

I agree with the comments above and also need that feature to get a configuration with a custom provider over HTTP calls.

niyazz avatar Mar 21 '23 15:03 niyazz

At least this ticket has only been open for 3.5 yrs.......

f135ta avatar May 19 '23 00:05 f135ta

I'm using Vault for getting secrets via http so async implementation is very useful such this scenarios.

MCKanpolat avatar May 22 '23 11:05 MCKanpolat

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.

dazinator avatar Jun 24 '23 09:06 dazinator

I have a similar scenario like @MCKanpolat. An async implementation would be really useful. Plus one for async!

stevo-knievo avatar Sep 20 '23 21:09 stevo-knievo

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 avatar Nov 10 '23 10:11 Edgaras91

@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.

Rabadash8820 avatar Nov 10 '23 18:11 Rabadash8820

@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.

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.

KieranDevvs avatar Nov 10 '23 19:11 KieranDevvs