diff --git a/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptOptions.cs b/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptOptions.cs
index 6c8fdb6..f8dc295 100644
--- a/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptOptions.cs
+++ b/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptOptions.cs
@@ -46,9 +46,9 @@ public class LetsEncryptOptions
///
public RenewalFailMode RenewalFailMode { get; set; } = RenewalFailMode.LogAndContinue;
- ///
- /// Gets or sets the used to request a new LetsEncrypt certificate.
- ///
- public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
+ ///
+ /// Gets or sets the used to request a new LetsEncrypt certificate.
+ ///
+ public KeyAlgorithm KeyAlgorithm { get; set; } = KeyAlgorithm.ES256;
}
}
diff --git a/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptRenewalService.cs b/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptRenewalService.cs
index efa28d3..f1ce00f 100644
--- a/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptRenewalService.cs
+++ b/src/FluffySpoon.AspNet.LetsEncrypt/Certes/LetsEncryptRenewalService.cs
@@ -4,6 +4,7 @@
using System.Threading;
using System.Threading.Tasks;
using FluffySpoon.AspNet.LetsEncrypt.Certificates;
+using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Logging;
using static FluffySpoon.AspNet.LetsEncrypt.Certificates.CertificateRenewalStatus;
@@ -14,6 +15,7 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
private readonly ICertificateProvider _certificateProvider;
private readonly IEnumerable _lifecycleHooks;
private readonly ILogger _logger;
+ private readonly IApplicationLifetime _lifetime;
private readonly SemaphoreSlim _semaphoreSlim;
private readonly LetsEncryptOptions _options;
@@ -22,11 +24,13 @@ public class LetsEncryptRenewalService : ILetsEncryptRenewalService
public LetsEncryptRenewalService(
ICertificateProvider certificateProvider,
IEnumerable lifecycleHooks,
+ IApplicationLifetime lifetime,
ILogger logger,
LetsEncryptOptions options)
{
_certificateProvider = certificateProvider;
_lifecycleHooks = lifecycleHooks;
+ _lifetime = lifetime;
_logger = logger;
_options = options;
_semaphoreSlim = new SemaphoreSlim(1);
@@ -45,10 +49,12 @@ public async Task StartAsync(CancellationToken cancellationToken)
" which means that the LetsEncrypt certificate will never renew.");
}
+ _lifetime.ApplicationStarted.Register(() => OnApplicationStarted(cancellationToken));
+
foreach (var lifecycleHook in _lifecycleHooks)
await lifecycleHook.OnStartAsync();
- _timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, TimeSpan.Zero, TimeSpan.FromHours(1));
+ _timer = new Timer(async state => await RunOnceWithErrorHandlingAsync(), null, Timeout.InfiniteTimeSpan, TimeSpan.FromHours(1));
}
public async Task StopAsync(CancellationToken cancellationToken)
@@ -93,19 +99,22 @@ public async Task RunOnceAsync()
private async Task RunOnceWithErrorHandlingAsync()
{
- try
- {
+ try {
await RunOnceAsync();
_timer?.Change(TimeSpan.FromHours(1), TimeSpan.FromHours(1));
- }
- catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled)
- {
+ } catch (Exception e) when (_options.RenewalFailMode != RenewalFailMode.Unhandled) {
_logger.LogWarning(e, $"Exception occured renewing certificates: '{e.Message}.'");
- if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry)
+ if (_options.RenewalFailMode == RenewalFailMode.LogAndRetry) {
_timer?.Change(TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1));
+ }
}
}
+ private void OnApplicationStarted(CancellationToken t) {
+ _logger.LogInformation("Application started");
+ _timer?.Change(TimeSpan.Zero, TimeSpan.FromHours(1));
+ }
+
public void Dispose()
{
_timer?.Dispose();
diff --git a/src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs b/src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs
index 8a7d000..65a11d8 100644
--- a/src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs
+++ b/src/FluffySpoon.AspNet.LetsEncrypt/RegistrationExtensions.cs
@@ -135,8 +135,9 @@ public static void AddFluffySpoonLetsEncrypt(
services.AddSingleton();
services.AddSingleton();
services.AddSingleton();
+
services.AddTransient();
- services.AddTransient();
+ services.AddTransient();
}
public static void UseFluffySpoonLetsEncrypt(