internal override void OnValidate()

in src/Microsoft.Azure.NotificationHubs/AppleTemplateRegistrationDescription.cs [173:276]


        internal override void OnValidate()
        {
            base.OnValidate();

            if (this.Expiry != null)
            {
                if (this.Expiry == string.Empty)
                {
                    throw new InvalidDataContractException(SRClient.EmptyExpiryValue);
                }

                if (ExpressionEvaluator.Validate(this.Expiry) == ExpressionEvaluator.ExpressionType.Literal)
                {
                    DateTime returnVal;
                    if (!DateTime.TryParse(this.Expiry, out returnVal) && !string.Equals("0", this.Expiry, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidDataContractException(SRClient.ExpiryDeserializationError);
                    }
                }
            }

            if (this.Priority != null)
            {
                if (this.Priority == string.Empty)
                {
                    throw new InvalidDataContractException(SRClient.EmptyPriorityValue);
                }

                if (ExpressionEvaluator.Validate(this.Priority) == ExpressionEvaluator.ExpressionType.Literal)
                {
                    byte returnVal;
                    if (!byte.TryParse(this.Priority, out returnVal))
                    {
                        throw new InvalidDataContractException(SRClient.PriorityDeserializationError);
                    }
                }
            }

            if (this.ApnsHeaders != null)
            {
                if (this.ApnsHeaders.ContainsKey(AppleRegistrationDescription.ApnsPriorityHeader))
                {
                    if (ExpressionEvaluator.Validate(this.ApnsHeaders[AppleRegistrationDescription.ApnsPriorityHeader]) == ExpressionEvaluator.ExpressionType.Literal)
                    {
                        byte returnVal;
                        if (!byte.TryParse(this.ApnsHeaders[AppleRegistrationDescription.ApnsPriorityHeader], out returnVal))
                        {
                            throw new InvalidDataContractException(SRClient.PriorityDeserializationError);
                        }
                    }
                }

                if (this.ApnsHeaders.ContainsKey(AppleRegistrationDescription.ApnsExpiryHeader))
                {
                    if (ExpressionEvaluator.Validate(this.ApnsHeaders[AppleRegistrationDescription.ApnsExpiryHeader]) == ExpressionEvaluator.ExpressionType.Literal)
                    {
                        int returnVal;
                        if (!int.TryParse(this.ApnsHeaders[AppleRegistrationDescription.ApnsExpiryHeader], out returnVal))
                        {
                            throw new InvalidDataContractException(SRClient.ApnsExpiryHeaderDeserializationError);
                        }
                    }
                }

                foreach (var header in this.ApnsHeaders)
                {
                    if (!header.Key.StartsWith(AppleRegistrationDescription.ApnsHeaderPrefix, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new InvalidDataContractException(string.Format(SRClient.ApnsHeaderDeserializationError, header.Key));
                    }

                    ExpressionEvaluator.Validate(header.Value);
                }
            }

            try
            {
                using (XmlReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(this.BodyTemplate), new XmlDictionaryReaderQuotas()))
                {
                    XDocument payloadDocument = XDocument.Load(reader);

                    foreach (XElement element in payloadDocument.Root.DescendantsAndSelf())
                    {
                        foreach (XAttribute attribute in element.Attributes())
                        {
                            ExpressionEvaluator.Validate(attribute.Value);
                        }

                        if (!element.HasElements && !string.IsNullOrEmpty(element.Value))
                        {
                            ExpressionEvaluator.Validate(element.Value);
                        }
                    }
                }
            }
            catch (InvalidOperationException)
            {
                // We get an ugly and misleading error message when this exception happens -> The XmlReader state should be Interactive.
                // Hence we are using a more friendlier error message
                throw new XmlException(SRClient.FailedToDeserializeBodyTemplate);
            }

            this.ValidateTemplateName();
        }