Review requirement of Microsoft.Extensions.Hosting
This commit is contained in:
parent
2a7ff864bf
commit
29b0e78cc1
5 changed files with 22 additions and 31 deletions
|
|
@ -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<IHost> HostProperty = AvaloniaProperty.Register<App, IHost>("Host");
|
||||
public static readonly StyledProperty<IServiceProvider> ServiceProviderProperty = AvaloniaProperty.Register<App, IServiceProvider>(nameof(ServiceProvider));
|
||||
|
||||
public IHost Host => GetValue(HostProperty);
|
||||
|
||||
public IServiceProvider Services => Host.Services;
|
||||
public IServiceProvider ServiceProvider => GetValue(ServiceProviderProperty);
|
||||
|
||||
public override void Initialize()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -10,7 +10,7 @@
|
|||
<ItemGroup>
|
||||
<PackageReference Include="Avalonia.ReactiveUI" />
|
||||
<PackageReference Include="Avalonia.Themes.Fluent" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
|
||||
<PackageReference Include="Microsoft.Extensions.Http" />
|
||||
</ItemGroup>
|
||||
|
||||
|
|
|
|||
|
|
@ -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<IViewFor<MainViewModel>, 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;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue