Skip to content

Commit 9329575

Browse files
authored
Merge pull request #3283 from Jack251970/dependency_injection_installer
Fix System.PlatformNotSupportedException & Add exception message box in App constructor
2 parents 30aa1fa + 406b196 commit 9329575

File tree

2 files changed

+56
-23
lines changed

2 files changed

+56
-23
lines changed

Flow.Launcher/App.xaml.cs

Lines changed: 54 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -35,31 +35,64 @@ public partial class App : IDisposable, ISingleInstanceApp
3535
public App()
3636
{
3737
// Initialize settings
38-
var storage = new FlowLauncherJsonStorage<Settings>();
39-
_settings = storage.Load();
40-
_settings.SetStorage(storage);
41-
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
38+
try
39+
{
40+
var storage = new FlowLauncherJsonStorage<Settings>();
41+
_settings = storage.Load();
42+
_settings.SetStorage(storage);
43+
_settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled();
44+
}
45+
catch (Exception e)
46+
{
47+
ShowErrorMsgBoxAndFailFast("Cannot load setting storage, please check local data directory", e);
48+
return;
49+
}
4250

4351
// Configure the dependency injection container
44-
var host = Host.CreateDefaultBuilder()
45-
.UseContentRoot(AppContext.BaseDirectory)
46-
.ConfigureServices(services => services
47-
.AddSingleton(_ => _settings)
48-
.AddSingleton(sp => new Updater(sp.GetRequiredService<IPublicAPI>(), Launcher.Properties.Settings.Default.GithubRepo))
49-
.AddSingleton<Portable>()
50-
.AddSingleton<SettingWindowViewModel>()
51-
.AddSingleton<IAlphabet, PinyinAlphabet>()
52-
.AddSingleton<StringMatcher>()
53-
.AddSingleton<Internationalization>()
54-
.AddSingleton<IPublicAPI, PublicAPIInstance>()
55-
.AddSingleton<MainViewModel>()
56-
.AddSingleton<Theme>()
57-
).Build();
58-
Ioc.Default.ConfigureServices(host.Services);
52+
try
53+
{
54+
var host = Host.CreateDefaultBuilder()
55+
.UseContentRoot(AppContext.BaseDirectory)
56+
.ConfigureServices(services => services
57+
.AddSingleton(_ => _settings)
58+
.AddSingleton(sp => new Updater(sp.GetRequiredService<IPublicAPI>(), Launcher.Properties.Settings.Default.GithubRepo))
59+
.AddSingleton<Portable>()
60+
.AddSingleton<SettingWindowViewModel>()
61+
.AddSingleton<IAlphabet, PinyinAlphabet>()
62+
.AddSingleton<StringMatcher>()
63+
.AddSingleton<Internationalization>()
64+
.AddSingleton<IPublicAPI, PublicAPIInstance>()
65+
.AddSingleton<MainViewModel>()
66+
.AddSingleton<Theme>()
67+
).Build();
68+
Ioc.Default.ConfigureServices(host.Services);
69+
}
70+
catch (Exception e)
71+
{
72+
ShowErrorMsgBoxAndFailFast("Cannot configure dependency injection container, please open new issue in Flow.Launcher", e);
73+
return;
74+
}
5975

6076
// Initialize the public API and Settings first
61-
API = Ioc.Default.GetRequiredService<IPublicAPI>();
62-
_settings.Initialize();
77+
try
78+
{
79+
API = Ioc.Default.GetRequiredService<IPublicAPI>();
80+
_settings.Initialize();
81+
}
82+
catch (Exception e)
83+
{
84+
ShowErrorMsgBoxAndFailFast("Cannot initialize api and settings, please open new issue in Flow.Launcher", e);
85+
return;
86+
}
87+
}
88+
89+
private static void ShowErrorMsgBoxAndFailFast(string message, Exception e)
90+
{
91+
// Firstly show users the message
92+
MessageBox.Show(e.ToString(), message, MessageBoxButton.OK, MessageBoxImage.Error);
93+
94+
// Flow cannot construct its App instance, so ensure Flow crashes w/ the exception info.
95+
Environment.FailFast(message, e);
6396
}
6497

6598
[STAThread]

Flow.Launcher/Flow.Launcher.csproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
<PackageReference Include="InputSimulator" Version="1.0.4" />
9393
<!-- Do not upgrade Microsoft.Extensions.DependencyInjection and Microsoft.Extensions.Hosting since we are .Net7.0 -->
9494
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
95-
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.0" />
95+
<PackageReference Include="Microsoft.Extensions.Hosting" Version="7.0.1" />
9696
<PackageReference Include="Microsoft.Toolkit.Uwp.Notifications" Version="7.1.3" />
9797
<PackageReference Include="Microsoft.Windows.CsWin32" Version="0.3.106">
9898
<PrivateAssets>all</PrivateAssets>
@@ -104,7 +104,7 @@
104104
<PackageReference Include="NHotkey.Wpf" Version="3.0.0" />
105105
<PackageReference Include="PropertyChanged.Fody" Version="3.4.0" />
106106
<PackageReference Include="SemanticVersioning" Version="3.0.0" />
107-
<PackageReference Include="TaskScheduler" Version="2.11.0" />
107+
<PackageReference Include="Jack251970.TaskScheduler" Version="2.12.1" />
108108
<PackageReference Include="VirtualizingWrapPanel" Version="2.1.1" />
109109
</ItemGroup>
110110

0 commit comments

Comments
 (0)