From 1ca5856dd9561c969d2b67e12ea6d86d07153426 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20=C3=96hlund?= Date: Tue, 22 Apr 2014 11:23:05 +0200 Subject: [PATCH] Making sure exceptions during license initalization doesn't abort startup Fixes #2058 --- .../Licensing/LicenseInitializer.cs | 20 +++++++++++-- .../Licensing/LicenseManager.cs | 30 +++++++++++++------ 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/src/NServiceBus.Core/Licensing/LicenseInitializer.cs b/src/NServiceBus.Core/Licensing/LicenseInitializer.cs index 97b267e69d6..ec0c22d4923 100644 --- a/src/NServiceBus.Core/Licensing/LicenseInitializer.cs +++ b/src/NServiceBus.Core/Licensing/LicenseInitializer.cs @@ -1,5 +1,7 @@ namespace NServiceBus.Licensing { + using System; + using Logging; using Pipeline; using Pipeline.Contexts; @@ -7,10 +9,22 @@ class LicenseInitializer : PipelineOverride, INeedInitialization { public void Init() { - LicenseManager.InitializeLicense(); + var expiredLicense = true; + try + { + LicenseManager.InitializeLicense(); + + expiredLicense = LicenseManager.HasLicenseExpired(); + } + catch (Exception ex) + { + //we only log here to prevent licensing issue to abort startup and cause production outages + Logger.Fatal("Failed to initialize the license",ex); + } + Configure.Component(DependencyLifecycle.InstancePerCall) - .ConfigureProperty(p => p.LicenseExpired, LicenseManager.HasLicenseExpired()); + .ConfigureProperty(p => p.LicenseExpired, expiredLicense); } @@ -18,5 +32,7 @@ public override void Override(BehaviorList behavi { behaviorList.Add(); } + + static ILog Logger = LogManager.GetLogger(typeof(LicenseInitializer)); } } \ No newline at end of file diff --git a/src/NServiceBus.Core/Licensing/LicenseManager.cs b/src/NServiceBus.Core/Licensing/LicenseManager.cs index 4299f695620..a15d0280cb8 100644 --- a/src/NServiceBus.Core/Licensing/LicenseManager.cs +++ b/src/NServiceBus.Core/Licensing/LicenseManager.cs @@ -1,5 +1,6 @@ namespace NServiceBus.Licensing { + using System; using System.Diagnostics; using System.Threading; using System.Windows.Forms; @@ -116,7 +117,7 @@ static string GetExistingLicense() string existingLicense; //look in HKCU - if (new RegistryLicenseStore().TryReadLicense(out existingLicense)) + if (UserSidChecker.IsNotSystemSid() && new RegistryLicenseStore().TryReadLicense(out existingLicense)) { return existingLicense; } @@ -127,7 +128,6 @@ static string GetExistingLicense() return existingLicense; } - return LicenseLocationConventions.TryFindLicenseText(); } @@ -142,23 +142,35 @@ public static License License { if (license == null) { - InitializeLicense(); + try + { + InitializeLicense(); + } + catch (Exception ex) + { + //we only log here to prevent licensing issue to abort startup and cause production outages + Logger.Fatal("Failed to initialize the license", ex); + } } var nsbLicense = new License { AllowedNumberOfWorkerNodes = int.MaxValue, MaxThroughputPerSecond = int.MaxValue, + ExpirationDate = DateTime.MaxValue }; - if (license.ExpirationDate.HasValue) + if (license != null) { - nsbLicense.ExpirationDate = license.ExpirationDate.Value; - } + if (license.ExpirationDate.HasValue) + { + nsbLicense.ExpirationDate = license.ExpirationDate.Value; + } - if (license.UpgradeProtectionExpiration.HasValue) - { - nsbLicense.UpgradeProtectionExpiration = license.UpgradeProtectionExpiration.Value; + if (license.UpgradeProtectionExpiration.HasValue) + { + nsbLicense.UpgradeProtectionExpiration = license.UpgradeProtectionExpiration.Value; + } } return nsbLicense;