Fix so that health check notifications without config don't cause all notification methods to fail

This commit is contained in:
Jeavon
2017-08-08 16:50:17 +01:00
parent fc8c750fb0
commit fb4af74916
4 changed files with 31 additions and 13 deletions

View File

@@ -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());

View File

@@ -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)
{

View File

@@ -4,6 +4,7 @@ namespace Umbraco.Web.HealthCheck.NotificationMethods
{
public interface IHealthCheckNotificatationMethod
{
bool Enabled { get; }
Task SendAsync(HealthCheckResults results);
}
}