App.xaml.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. using Microsoft.Extensions.DependencyInjection;
  2. using Microsoft.Extensions.Hosting;
  3. using Splat.Microsoft.Extensions.DependencyInjection;
  4. using System;
  5. using System.Windows;
  6. using Volo.Abp;
  7. #pragma warning disable VSTHRD100 // 避免使用 Async Void 方法
  8. namespace NatTypeTester;
  9. public partial class App
  10. {
  11. private readonly IHost _host;
  12. private readonly IAbpApplicationWithExternalServiceProvider _application;
  13. public App()
  14. {
  15. _host = CreateHostBuilder();
  16. _application = _host.Services.GetRequiredService<IAbpApplicationWithExternalServiceProvider>();
  17. }
  18. protected override async void OnStartup(StartupEventArgs e)
  19. {
  20. try
  21. {
  22. await _host.StartAsync();
  23. Initialize(_host.Services);
  24. _host.Services.GetRequiredService<MainWindow>().Show();
  25. }
  26. catch (Exception ex)
  27. {
  28. MessageBox.Show(ex.Message, nameof(NatTypeTester), MessageBoxButton.OK, MessageBoxImage.Error);
  29. }
  30. }
  31. protected override async void OnExit(ExitEventArgs e)
  32. {
  33. _application.Shutdown();
  34. await _host.StopAsync();
  35. _host.Dispose();
  36. }
  37. private void Initialize(IServiceProvider serviceProvider)
  38. {
  39. _application.Initialize(serviceProvider);
  40. serviceProvider.UseMicrosoftDependencyResolver();
  41. }
  42. private static IHost CreateHostBuilder()
  43. {
  44. return Host.CreateDefaultBuilder()
  45. .UseAutofac()
  46. .ConfigureServices((_, services) => services.AddApplication<NatTypeTesterModule>())
  47. .Build();
  48. }
  49. }