/// <summary>
        /// Ensures that the provided connection string is valid and builds the connection string
        /// to be used for DDR connection to the given shard provider.
        /// </summary>
        /// <param name="shardProvider">Shard provider containing shard to be connected to.</param>
        /// <param name="connectionString">Input connection string.</param>
        /// <returns>Connection string for DDR connection.</returns>
        private string ValidateAndPrepareConnectionString(IShardProvider shardProvider, string connectionString)
        {
            Debug.Assert(shardProvider != null);
            Debug.Assert(connectionString != null);

            // Devnote: If connection string specifies Active Directory authentication and runtime is not
            // .NET 4.6 or higher, then below call will throw.
            SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

            // DataSource must not be set.
            if (!string.IsNullOrEmpty(connectionStringBuilder.DataSource))
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed,
                              "DataSource"),
                          "connectionString");
            }

            // InitialCatalog must not be set.
            if (!string.IsNullOrEmpty(connectionStringBuilder.InitialCatalog))
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed,
                              "Initial Catalog"),
                          "connectionString");
            }

            // ConnectRetryCount must not be set (default value is 1)
            if (ShardMapUtils.IsConnectionResiliencySupported && (int)connectionStringBuilder[ShardMapUtils.ConnectRetryCount] > 1)
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._ShardMap_OpenConnection_ConnectionStringPropertyDisallowed,
                              ShardMapUtils.ConnectRetryCount),
                          "connectionString");
            }

            // Verify that either UserID/Password or provided or integrated authentication is enabled.
            SqlShardMapManagerCredentials.EnsureCredentials(connectionStringBuilder, "connectionString");

            Shard s = shardProvider.ShardInfo;

            connectionStringBuilder.DataSource     = s.Location.DataSource;
            connectionStringBuilder.InitialCatalog = s.Location.Database;

            // Append the proper post-fix for ApplicationName
            connectionStringBuilder.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix(
                connectionStringBuilder.ApplicationName,
                this.ApplicationNameSuffix);

            // Disable connection resiliency if necessary
            if (ShardMapUtils.IsConnectionResiliencySupported)
            {
                connectionStringBuilder[ShardMapUtils.ConnectRetryCount] = 0;
            }

            return(connectionStringBuilder.ConnectionString);
        }
        /// <summary>
        /// Instantiates the object that holds the credentials for accessing SQL Servers
        /// containing the shard map manager data.
        /// </summary>
        /// <param name="connectionString">Connection string for Shard map manager data source.</param>
        public SqlShardMapManagerCredentials(string connectionString)
        {
            ExceptionUtils.DisallowNullArgument(connectionString, "connectionString");

            // Devnote: If connection string specifies Active Directory authentication and runtime is not
            // .NET 4.6 or higher, then below call will throw.
            SqlConnectionStringBuilder connectionStringBuilder = new SqlConnectionStringBuilder(connectionString);

            #region GSM Validation

            // DataSource must be set.
            if (string.IsNullOrEmpty(connectionStringBuilder.DataSource))
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired,
                              "DataSource"),
                          "connectionString");
            }

            // InitialCatalog must be set.
            if (string.IsNullOrEmpty(connectionStringBuilder.InitialCatalog))
            {
                throw new ArgumentException(
                          StringUtils.FormatInvariant(
                              Errors._SqlShardMapManagerCredentials_ConnectionStringPropertyRequired,
                              "Initial Catalog"),
                          "connectionString");
            }

            // Ensure credentials are specified for GSM connectivity.
            SqlShardMapManagerCredentials.EnsureCredentials(connectionStringBuilder, "connectionString");

            #endregion GSM Validation

            // Copy the input connection strings.
            _connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString);

            _connectionStringShardMapManager.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix(
                _connectionStringShardMapManager.ApplicationName,
                GlobalConstants.ShardMapManagerInternalConnectionSuffixGlobal);

            _connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString);

            _connectionStringShard.Remove("Data Source");
            _connectionStringShard.Remove("Initial Catalog");

            _connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix(
                _connectionStringShard.ApplicationName,
                GlobalConstants.ShardMapManagerInternalConnectionSuffixLocal);
        }