HostHolder.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 
  2. using Blazorise;
  3. using Blazorise.Bootstrap5;
  4. using Blazorise.Icons.FontAwesome;
  5. using MudBlazor.Services;
  6. using MudExtensions.Services;
  7. using PCC2.AssemblyInject;
  8. using PCC2.EventBus;
  9. using PCC2.Init;
  10. using PCC2.Logging;
  11. namespace PCC2.AppScaffold;
  12. public class HostHolder
  13. {
  14. private static IHost HoldedHost;
  15. public static IServiceProvider ServiceProvider { get; private set; }
  16. public static IAsyncEventBus EventBus { get; private set; }
  17. public static void BuildHost()
  18. {
  19. var builder = Host.CreateApplicationBuilder();
  20. // 注册其他服务
  21. var services = builder.Services;
  22. services.AddAssemblyInject<HostHolder>();
  23. services.AddWindowsFormsBlazorWebView();
  24. //界面框架
  25. services.AddMudServices();
  26. services.AddMudExtensions();
  27. services.AddBlazorise(options => options.Immediate = true)
  28. .AddBootstrap5Providers()
  29. .AddFontAwesomeIcons();
  30. // 日志配置
  31. builder.SetLogFormatForSimpleConsole(false);
  32. var loggingIntoEventBus = new LoggingIntoEventBus();
  33. services.AddLogging(opt =>
  34. {
  35. opt.AddProvider(loggingIntoEventBus);
  36. opt.AddDebug();
  37. });
  38. HoldedHost = builder.Build();
  39. ServiceProvider = HoldedHost.Services;
  40. EventBus = ServiceProvider.GetRequiredService<IAsyncEventBus>();
  41. loggingIntoEventBus.EventBus = EventBus;
  42. LoggingHolder.Init(EventBus);
  43. }
  44. public static async Task StartAsync() => await HoldedHost.StartAsync();
  45. public static async Task StopAsync() => await HoldedHost.StopAsync();
  46. }