diff --git a/Directory.Build.props b/Directory.Build.props new file mode 100644 index 0000000..ac8ac4d --- /dev/null +++ b/Directory.Build.props @@ -0,0 +1,8 @@ + + + + + true + + + \ No newline at end of file diff --git a/Directory.Packages.props b/Directory.Packages.props index a950adb..43990f0 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -17,7 +17,6 @@ - diff --git a/app/InkForge.Common/App.axaml.cs b/app/InkForge.Common/App.axaml.cs index b92102b..fa1c765 100644 --- a/app/InkForge.Common/App.axaml.cs +++ b/app/InkForge.Common/App.axaml.cs @@ -3,19 +3,15 @@ using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using Avalonia.Markup.Xaml; -using Microsoft.Extensions.Hosting; - using ReactiveUI; namespace InkForge.Common; public partial class App : Application { - public static readonly StyledProperty HostProperty = AvaloniaProperty.Register("Host"); + public static readonly StyledProperty ServiceProviderProperty = AvaloniaProperty.Register(nameof(ServiceProvider)); - public IHost Host => GetValue(HostProperty); - - public IServiceProvider Services => Host.Services; + public IServiceProvider ServiceProvider => GetValue(ServiceProviderProperty); public override void Initialize() { diff --git a/app/InkForge.Common/InkForge.Common.csproj b/app/InkForge.Common/InkForge.Common.csproj index cc5502b..cee79e5 100644 --- a/app/InkForge.Common/InkForge.Common.csproj +++ b/app/InkForge.Common/InkForge.Common.csproj @@ -10,7 +10,7 @@ - + diff --git a/app/InkForge.Desktop/Program.cs b/app/InkForge.Desktop/Program.cs index 4b67b25..23e8531 100644 --- a/app/InkForge.Desktop/Program.cs +++ b/app/InkForge.Desktop/Program.cs @@ -5,7 +5,6 @@ using Avalonia.Threading; using InkForge.Common; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Hosting; using ReactiveUI; @@ -17,7 +16,7 @@ static class Program [STAThread] public static void Main(string[] args) => BuildAvaloniaApp() - .UseMicrosoftExtensionsHosting(args) + .UseMicrosoftDependencyInjection(args) .StartWithClassicDesktopLifetime(args); public static AppBuilder BuildAvaloniaApp() @@ -40,39 +39,28 @@ static class Program // services.AddTransient, MainWindow>(); } - private static void OnSetup(this HostApplicationBuilder hostBuilder, AppBuilder appBuilder) + private static void OnSetup(this IServiceCollection services, AppBuilder appBuilder) { var dispatcher = Dispatcher.UIThread; var app = appBuilder.Instance!; - hostBuilder.Services + services .AddSingleton(app) .AddSingleton(app.ApplicationLifetime!) .AddSingleton(app.PlatformSettings!) .AddSingleton(dispatcher); - var host = hostBuilder.Build(); - host.Services.UseMicrosoftDependencyResolver(); - app.SetValue(App.HostProperty, host); - dispatcher.ShutdownStarted += host.Shutdown; - - dispatcher.Post(static arg => - { - var host = (IHost)arg!; - host.StartAsync() - .GetAwaiter() - .GetResult(); - }, host, DispatcherPriority.Send); + var serviceProvider = services.BuildServiceProvider(); + serviceProvider.UseMicrosoftDependencyResolver(); + app.SetValue(App.ServiceProviderProperty, serviceProvider); + dispatcher.ShutdownFinished += (_, _) => serviceProvider.Dispose(); } - private static void Shutdown(this IHost host, object? sender, EventArgs e) - => host.StopAsync().GetAwaiter().GetResult(); - - private static AppBuilder UseMicrosoftExtensionsHosting(this AppBuilder builder, string[] args) + private static AppBuilder UseMicrosoftDependencyInjection(this AppBuilder builder, string[] args) { - var hostBuilder = Host.CreateApplicationBuilder(args); - ConfigureServices(hostBuilder.Services); - builder.AfterSetup(hostBuilder.OnSetup); + ServiceCollection services = []; + ConfigureServices(services); + builder.AfterSetup(services.OnSetup); return builder; } }