Added IAsyncInitialization to host startup
This commit is contained in:
@@ -2,19 +2,24 @@
|
|||||||
|
|
||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class AppService(IEnumerable<IInitialization> initializers,
|
public class AppService(IEnumerable<IInitialization> initializations,
|
||||||
|
IEnumerable<IAsyncInitialization> asyncInitializations,
|
||||||
IPublisher publisher) :
|
IPublisher publisher) :
|
||||||
IHostedService
|
IHostedService
|
||||||
{
|
{
|
||||||
public Task StartAsync(CancellationToken cancellationToken)
|
public async Task StartAsync(CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
foreach (IInitialization initializer in initializers)
|
foreach (IInitialization initialization in initializations)
|
||||||
{
|
{
|
||||||
initializer.Initialize();
|
initialization.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (IAsyncInitialization initialization in asyncInitializations)
|
||||||
|
{
|
||||||
|
await initialization.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
publisher.Publish<StartedEventArgs>();
|
publisher.Publish<StartedEventArgs>();
|
||||||
return Task.CompletedTask;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
public Task StopAsync(CancellationToken cancellationToken) => Task.CompletedTask;
|
||||||
|
|||||||
@@ -4,7 +4,8 @@ using Microsoft.Extensions.Hosting;
|
|||||||
namespace Toolkit.Foundation;
|
namespace Toolkit.Foundation;
|
||||||
|
|
||||||
public class ComponentHost(IServiceProvider services,
|
public class ComponentHost(IServiceProvider services,
|
||||||
IEnumerable<IInitialization> initializers,
|
IEnumerable<IInitialization> initializations,
|
||||||
|
IEnumerable<IAsyncInitialization> asyncInitializations,
|
||||||
IEnumerable<IHostedService> hostedServices) :
|
IEnumerable<IHostedService> hostedServices) :
|
||||||
IComponentHost
|
IComponentHost
|
||||||
{
|
{
|
||||||
@@ -22,9 +23,14 @@ public class ComponentHost(IServiceProvider services,
|
|||||||
|
|
||||||
public async Task StartAsync(CancellationToken cancellationToken = default)
|
public async Task StartAsync(CancellationToken cancellationToken = default)
|
||||||
{
|
{
|
||||||
foreach (IInitialization initializer in initializers)
|
foreach (IInitialization initialization in initializations)
|
||||||
{
|
{
|
||||||
initializer.Initialize();
|
initialization.Initialize();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach (IAsyncInitialization initialization in asyncInitializations)
|
||||||
|
{
|
||||||
|
await initialization.Initialize();
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach (IHostedService service in hostedServices)
|
foreach (IHostedService service in hostedServices)
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ public class DefaultHostBuilder :
|
|||||||
services.AddHandler<NavigateHandler>();
|
services.AddHandler<NavigateHandler>();
|
||||||
services.AddHandler<NavigateBackHandler>();
|
services.AddHandler<NavigateBackHandler>();
|
||||||
|
|
||||||
services.AddInitializer<ComponentInitializer>();
|
services.AddInitialization<ComponentInitializer>();
|
||||||
services.AddHostedService<AppService>();
|
services.AddHostedService<AppService>();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -90,11 +90,19 @@ public static class IServiceCollectionExtensions
|
|||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static IServiceCollection AddInitializer<TInitializer>(this IServiceCollection services)
|
public static IServiceCollection AddInitialization<TInitialization>(this IServiceCollection services)
|
||||||
where TInitializer : class,
|
where TInitialization : class,
|
||||||
IInitialization
|
IInitialization
|
||||||
{
|
{
|
||||||
services.AddTransient<IInitialization, TInitializer>();
|
services.AddTransient<IInitialization, TInitialization>();
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddAsyncInitialization<TInitialization>(this IServiceCollection services)
|
||||||
|
where TInitialization : class,
|
||||||
|
IAsyncInitialization
|
||||||
|
{
|
||||||
|
services.AddTransient<IAsyncInitialization, TInitialization>();
|
||||||
return services;
|
return services;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user