Hacker News new | ask | show | jobs
by shireboy 655 days ago
If I understand the problem, just move all your DI registrations to a shared extension method:

  public static ConfigurationExtensions{
      public static AddMyApp(this IServiceCollection services){
           services.AddScoped<IFooService,FooService>();
      }
  }

  //In console app:
  var consoleBuilder = Host.CreateApplicationBuilder(args);
  consoleBuilder.Services.AddMyApp();
  ...
  //pseudocode - in real world you'd put this in another class or method called by the commandline code:
  if(webHostCommandLineSwitch){
      var webBuilder = WebApplication.CreateBuilder(args);
      webBuilder.Services.AddMyApp();
     ...
  }