Fix so that health check notifications without config don't cause all notification methods to fail
This commit is contained in:
@@ -59,20 +59,37 @@ namespace Umbraco.Web.HealthCheck
|
||||
var healthCheckConfig = UmbracoConfig.For.HealthCheck();
|
||||
var notificationMethods = healthCheckConfig.NotificationSettings.NotificationMethods;
|
||||
var notificationMethod = notificationMethods[attribute.Alias];
|
||||
if (notificationMethod == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
// Create array for constructor paramenters. Will consists of common ones that all notification methods have as well
|
||||
// as those specific to this particular notification method.
|
||||
var baseType = typeof(NotificationMethodBase);
|
||||
var baseTypeCtor = baseType.GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance).First();
|
||||
var baseTypeCtorParamNames = baseTypeCtor.GetParameters().Select(x => x.Name);
|
||||
var ctorParams = new List<object> { notificationMethod.Enabled, notificationMethod.FailureOnly, notificationMethod.Verbosity };
|
||||
ctorParams.AddRange(ctor.GetParameters()
|
||||
.Where(x => baseTypeCtorParamNames.Contains(x.Name) == false)
|
||||
.Select(x => notificationMethod.Settings[x.Name].Value));
|
||||
|
||||
List<object> ctorParams;
|
||||
|
||||
if (notificationMethod != null)
|
||||
{
|
||||
// configuration found so set ctorParams to config values
|
||||
ctorParams = new List<object>
|
||||
{
|
||||
notificationMethod.Enabled,
|
||||
notificationMethod.FailureOnly,
|
||||
notificationMethod.Verbosity
|
||||
};
|
||||
ctorParams.AddRange(ctor.GetParameters()
|
||||
.Where(x => baseTypeCtorParamNames.Contains(x.Name) == false)
|
||||
.Select(x => notificationMethod.Settings[x.Name].Value));
|
||||
}
|
||||
else
|
||||
{
|
||||
// no configuration found so set to default values, enabled = false
|
||||
ctorParams = new List<object> { false, false, HealthCheckNotificationVerbosity.Detailed };
|
||||
ctorParams.AddRange(ctor.GetParameters()
|
||||
.Where(x => baseTypeCtorParamNames.Contains(x.Name) == false)
|
||||
.Select(x => string.Empty));
|
||||
}
|
||||
|
||||
// Instantiate the type with the constructor parameters
|
||||
return Activator.CreateInstance(serviceType, ctorParams.ToArray());
|
||||
|
||||
@@ -23,7 +23,7 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
public EmailNotificationMethod(bool enabled, bool failureOnly, HealthCheckNotificationVerbosity verbosity,
|
||||
string recipientEmail)
|
||||
: this(enabled, failureOnly, verbosity, recipientEmail, ApplicationContext.Current.Services.TextService)
|
||||
{
|
||||
{
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -39,13 +39,13 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
: base(enabled, failureOnly, verbosity)
|
||||
{
|
||||
if (textService == null) throw new ArgumentNullException("textService");
|
||||
if (string.IsNullOrWhiteSpace(recipientEmail)) throw new ArgumentException("Value cannot be null or whitespace.", "recipientEmail");
|
||||
if (enabled && string.IsNullOrWhiteSpace(recipientEmail)) throw new ArgumentException("Value cannot be null or whitespace.", "recipientEmail");
|
||||
_textService = textService;
|
||||
RecipientEmail = recipientEmail;
|
||||
Verbosity = verbosity;
|
||||
}
|
||||
|
||||
public string RecipientEmail { get; private set; }
|
||||
public string RecipientEmail { get; private set; }
|
||||
|
||||
public async Task SendAsync(HealthCheckResults results)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods
|
||||
{
|
||||
public interface IHealthCheckNotificatationMethod
|
||||
{
|
||||
bool Enabled { get; }
|
||||
Task SendAsync(HealthCheckResults results);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user