Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

290
Views
how to add a provider in startup.cs not overwriting all pre-existing providers?

I saw code to add a provider (let's say the extention method to add this provider is called AddXXX) in startup.cs:

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()  // have to do tedious setup which can be done by program.cs
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", 
                     optional: false, 
                     reloadOnChange: true)
        .AddEnvironmentVariables();

    if (env.IsDevelopment())
    {
        builder.AddXXX();
    }

    Configuration = builder.Build();   
    ... // setup other middlewares
}

But considering we have already used a default hostbuilder in program.cs:

public class Program
{
   public static void Main(string[] args) {
      CreateHostBuilder(args).Build().Run();
   }

   public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args)
           .ConfigureWebHostDefaults(webBuilder => {
              webBuilder.UseStartup<Startup>();
           });
}

I don't want to configure ConfigurationBuilder again, which is quite tedious, e.g add json file provider which is already added by CreateDefaultBuilder, so I feel that the most efficient way is to add the provider in program.cs as:

public class Program
{
   public static void Main(string[] args) {
      CreateHostBuilder(args).Build().Run();
   }

   public static IHostBuilder CreateHostBuilder(string[] args) =>
       Host.CreateDefaultBuilder(args)
           .ConfigureAppConfiguration((hostingContext, config) => {
              if (hostingContext.HostingEnvironment.IsDevelopment()) {
                 config.AddXXX();
              }
           })
           .ConfigureWebHostDefaults(webBuilder => {
              webBuilder.UseStartup<Startup>();
           });
}

Is my thinking correct?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

From here

... the following service types can be injected into the Startup constructor when using the Generic Host (IHostBuilder):

  • IWebHostEnvironment
  • IHostEnvironment
  • IConfiguration

So what you can do is;

public class Startup
{
    private readonly IWebHostEnvironment _env;
    private readonly IConfiguration _config;

    public Startup(IConfiguration configuration, IWebHostEnvironment env)
    {
        _config = configuration;
        _env = env;
    }
    ...
}

With environment specific configuration defined in your CreateHostBuilder method, as in your question.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!