private static NameValueCollection CreateNameValueCollectionFromConnectionString()

in src/Microsoft.Azure.NotificationHubs/KeyValueConfigurationManager.cs [54:112]


        private static NameValueCollection CreateNameValueCollectionFromConnectionString(string connectionString)
        {
            var settings = new NameValueCollection();
            if (!string.IsNullOrWhiteSpace(connectionString))
            {
                var connection = KeyDelimiter + connectionString;
                var keyValues = Regex.Split(connection, KeyDelimiterRegexString, RegexOptions.IgnoreCase);
                if (keyValues.Length > 0)
                {
                    // Regex.Split returns the array that include part of the delimiters, so it will look 
                    // something like this:
                    // { "", "Endpoint", "sb://a.b.c", "OperationTimeout", "01:20:30", ...}
                    // We should always get empty string for first element (except if we found no match at all).
                    if (!string.IsNullOrWhiteSpace(keyValues[0]))
                    {
                        throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigSettingInvalidKey, connectionString));
                    }

                    if (keyValues.Length % 2 != 1)
                    {
                        throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigSettingInvalidKey, connectionString));
                    }

                    for (var i = 1; i < keyValues.Length; i++)
                    {
                        var key = keyValues[i];
                        if (string.IsNullOrWhiteSpace(key) || !KeyRegex.IsMatch(key))
                        {
                            throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigSettingInvalidKey, key));
                        }

                        var value = keyValues[i + 1];
                        if (string.IsNullOrWhiteSpace(value) || !ValueRegex.IsMatch(value))
                        {
                            throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigSettingInvalidValue, key, value));
                        }

                        if (settings[key] != null)
                        {
                            throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigDuplicateSetting, key));
                        }

                        settings[key] = value;
                        i++;
                    }
                }
            }

            return settings;
        }

        public void Validate()
        {
            if (string.IsNullOrWhiteSpace(connectionProperties[EndpointConfigName]))
            {
                throw new ConfigurationException(string.Format(SRClient.AppSettingsConfigMissingSetting, EndpointConfigName));
            }
        }
    }