public async Task RunOnceAsync()
        {
            if (_semaphoreSlim.CurrentCount == 0)
            {
                return;
            }

            await _semaphoreSlim.WaitAsync();

            try
            {
                var result = await _certificateProvider.RenewCertificateIfNeeded(Certificate);

                if (result.Status != Unchanged)
                {
                    // Preload intermediate certs before exposing certificate to the Kestrel
                    using var chain = new X509Chain
                          {
                              ChainPolicy =
                              {
                                  RevocationMode = X509RevocationMode.NoCheck
                              }
                          };

                    if (chain.Build(result.Certificate))
                    {
                        _logger.LogInformation("Successfully built certificate chain");
                    }
                    else
                    {
                        _logger.LogWarning("Was not able to build certificate chain. This can cause an outage of your app.");
                    }
                }

                Certificate = result.Certificate;

                if (result.Status == Renewed)
                {
                    foreach (var lifecycleHook in _lifecycleHooks)
                    {
                        await lifecycleHook.OnRenewalSucceededAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                foreach (var lifecycleHook in _lifecycleHooks)
                {
                    await lifecycleHook.OnExceptionAsync(ex);
                }

                throw;
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }
Esempio n. 2
0
        public async Task RunOnceAsync()
        {
            if (_semaphoreSlim.CurrentCount == 0)
            {
                return;
            }

            await _semaphoreSlim.WaitAsync();

            try
            {
                var result = await _certificateProvider.RenewCertificateIfNeeded(Certificate);

                Certificate = result.Certificate;

                if (result.Status == Renewed)
                {
                    foreach (var lifecycleHook in _lifecycleHooks)
                    {
                        await lifecycleHook.OnRenewalSucceededAsync();
                    }
                }
            }
            catch (Exception ex)
            {
                foreach (var lifecycleHook in _lifecycleHooks)
                {
                    await lifecycleHook.OnExceptionAsync(ex);
                }

                throw;
            }
            finally
            {
                _semaphoreSlim.Release();
            }
        }