本文整理汇总了C#中Catel.Data.ValidationContext.GetErrors方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationContext.GetErrors方法的具体用法?C# ValidationContext.GetErrors怎么用?C# ValidationContext.GetErrors使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Catel.Data.ValidationContext
的用法示例。
在下文中一共展示了ValidationContext.GetErrors方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateLicense
/// <summary>
/// Validates the license.
/// </summary>
/// <param name="license">The license key the user has given to be validated.</param>
/// <returns>The validation context containing all the validation results.</returns>
public IValidationContext ValidateLicense(string license)
{
Argument.IsNotNullOrWhitespace(() => license);
var validationContext = new ValidationContext();
Log.Info("Validating license");
try
{
var licenseObject = License.Load(license);
var failureList = licenseObject.Validate()
.Signature(_applicationIdService.ApplicationId)
.AssertValidLicense().ToList();
if (failureList.Count > 0)
{
foreach (var failure in failureList)
{
var businessRuleValidationResult = BusinessRuleValidationResult.CreateErrorWithTag(failure.Message, failure.HowToResolve);
validationContext.AddBusinessRuleValidationResult(businessRuleValidationResult);
}
}
var licenseAttributes = licenseObject.AdditionalAttributes;
if (licenseAttributes != null)
{
foreach (var licenseAttribute in licenseAttributes.GetAll())
{
if (string.Equals(licenseAttribute.Key, LicenseElements.MachineId))
{
Log.Debug("Validating license using machine ID");
var machineLicenseValidationContext = _machineLicenseValidationService.Validate(licenseAttribute.Value);
validationContext.SynchronizeWithContext(machineLicenseValidationContext, true);
if (machineLicenseValidationContext.HasErrors)
{
Log.Error("The license can only run on machine with ID '{0}'", licenseAttribute.Value);
}
}
// TODO: add additional attribute checks here
}
}
// Also validate the xml, very important for expiration date and version
var xmlValidationContext = ValidateXml(license);
validationContext.SynchronizeWithContext(xmlValidationContext, true);
}
catch (Exception ex)
{
Log.Error(ex, "An error occurred while loading the license");
validationContext.AddBusinessRuleValidationResult(BusinessRuleValidationResult.CreateError("An unknown error occurred while loading the license, please contact support"));
}
finally
{
if (validationContext.GetErrors().Count > 0)
{
Log.Warning("License is not valid:");
Log.Indent();
foreach (var error in validationContext.GetErrors())
{
Log.Warning("- {0}\n{1}", error.Message, error.Tag as string);
}
Log.Unindent();
}
else
{
Log.Info("License is valid");
}
}
return validationContext;
}