0

I'm using library that creates builder in this way

var webApiOptions = new AkaAppOptions<ApiOptions>(ApplicationId, authenticationType);
var builder = new AkaAppBuilder<ApiOptions>(args, webApiOptions);

now this builder contains IConfiguration that I need.

My problem is that I need to read configuration to know what kind of authenticationType it is. this happens before I have a WebApplicationBuilder.

My options are to create temporary WebApplicationBuilder, read configuration and then create this library builder, drawback here is memory I guess.

And another option is to read appsettings.json manually to get that information/ drawback is memory and memory environment appsettings?

Which one to read

So for example I would do something like this to create temporary builder and than the one I need

var tempBuilder = WebApplication.CreateBuilder(args);
var useExternalAuth = tempBuilder.Configuration.GetSection("ExternalAuth").Exists();
var authenticationType = useExternalAuth ? AuthenticationType.ExternalService : AuthenticationType.Service;

var webApiOptions = new AkaAppOptions<ApiOptions>(ApplicationId, authenticationType);
var builder = new AkaAppBuilder<ApiOptions>(args, webApiOptions);

Are there any other options?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Please give an example of how you want to use it. Often the configuration options of the extension methods to add things to the builder offer a builder context, from which you can access the configuration. So the answer depends on the specific use case – JHBonarius Oct 05 '22 at 11:16
  • Your design choices kind of limit the flexibility you have. However, we cannot help you there as you are not showing a [mcve]. Usually one would use an [Options pattern](https://learn.microsoft.com/en-us/aspnet/core/fundamentals/configuration/options) for something like this. – JHBonarius Oct 05 '22 at 13:36
  • @JHBonarius It absolutely does, thanks. I did provide example and since this is program.cs this is all the exmaple I can provide. can't use options pattern here – edward ewerus Oct 05 '22 at 13:59

1 Answers1

5

If it is only about the (temporary) configuration, you can set up a ConfigurationBuilder and add the required providers, e.g.:

IConfiguration config = new ConfigurationBuilder()
    .AddJsonFile("appsettings.json")
    .AddEnvironmentVariables()
    .Build();
// ... use temporary config

By calling build, the IConfiguration object is assembled and you can use it during the setup phase of your application.

For details on the configuration concepts in .NET Core, see this link.

Markus
  • 20,838
  • 4
  • 31
  • 55