Program.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using Microsoft.AspNetCore;
  2. using Microsoft.AspNetCore.Hosting;
  3. using Microsoft.Extensions.Configuration;
  4. using Microsoft.Extensions.Hosting;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using System;
  7. using System.IO;
  8. using DutchTreat.Data;
  9. using System.Linq;
  10. namespace DutchTreat
  11. {
  12. public class Program
  13. {
  14. public static void Main(string[] args)
  15. {
  16. //CreateHostBuilder(args).Build().Run();
  17. var host = BuildWebHost(args);
  18. if (args.Length > 0)
  19. {
  20. bool seed = args.Any(arg => arg == "/seed");
  21. if (seed)
  22. {
  23. RunSeeding(host);
  24. }
  25. }
  26. else
  27. {
  28. host.Run();
  29. }
  30. }
  31. private static void RunSeeding(IWebHost host)
  32. {
  33. var scopeFactory = host.Services.GetService<IServiceScopeFactory>();
  34. using (var scope = scopeFactory.CreateScope())
  35. {
  36. var seeder = scope.ServiceProvider.GetService<DutchSeeder>();
  37. seeder.SeedAsync().Wait();
  38. }
  39. }
  40. public static IWebHost BuildWebHost(string[] args) =>
  41. WebHost.CreateDefaultBuilder(args)
  42. .ConfigureAppConfiguration(SetupConfiguration)
  43. .UseStartup<Startup>()
  44. .Build();
  45. private static void SetupConfiguration(WebHostBuilderContext ctx, IConfigurationBuilder builder)
  46. {
  47. builder.Sources.Clear();
  48. builder.AddJsonFile("config.json", false, true)
  49. .AddEnvironmentVariables();
  50. //builder.SetBasePath(Directory.GetCurrentDirectory())
  51. // .AddJsonFile("config.json")
  52. // .AddEnvironmentVariables();
  53. }
  54. }
  55. }