Add lifetime hosted service to detect fully started ASP.NET Core

This commit is contained in:
Konstantin Gross
2020-05-24 02:51:34 +02:00
parent 4ad39ba4c1
commit cac9cfcec8
4 changed files with 54 additions and 2 deletions

View File

@@ -6,6 +6,7 @@ using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace ElectronNET.API
{
@@ -352,6 +353,11 @@ namespace ElectronNET.API
private event Action<bool> _accessibilitySupportChanged;
/// <summary>
/// Application host fully started.
/// </summary>
public bool IsReady { get; internal set; }
/// <summary>
/// A property that indicates the current application's name, which is the
/// name in the application's `package.json` file.

View File

@@ -0,0 +1,41 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Hosting;
namespace ElectronNET.API
{
/// <summary>
/// Base class that reports if ASP.NET Core has fully started.
/// </summary>
internal class LifetimeServiceHost : IHostedService
{
public LifetimeServiceHost(IHostApplicationLifetime lifetime)
{
lifetime.ApplicationStarted.Register(() =>
{
App.Instance.IsReady = true;
Console.WriteLine("ASP.NET Core host has fully started.");
});
}
/// <summary>
/// Triggered when the application host is ready to start the service.
/// </summary>
/// <param name="cancellationToken">Indicates that the start process has been aborted.</param>
public Task StartAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
/// <summary>
/// Triggered when the application host is performing a graceful shutdown.
/// </summary>
/// <param name="cancellationToken">Indicates that the shutdown process should no longer be graceful.</param>
public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}

View File

@@ -1,6 +1,7 @@
using Microsoft.AspNetCore.Hosting;
using System;
using System.IO;
using Microsoft.Extensions.DependencyInjection;
namespace ElectronNET.API
{
@@ -32,6 +33,11 @@ namespace ElectronNET.API
if (HybridSupport.IsElectronActive)
{
builder.ConfigureServices(services =>
{
services.AddHostedService<LifetimeServiceHost>();
});
// check for the content folder if its exists in base director otherwise no need to include
// It was used before because we are publishing the project which copies everything to bin folder and contentroot wwwroot was folder there.
// now we have implemented the live reload if app is run using /watch then we need to use the default project path.
@@ -49,4 +55,4 @@ namespace ElectronNET.API
return builder;
}
}
}
}