Esempio n. 1
0
        /// <summary>
        /// Instantiates the object that holds the credentials for accessing SQL Servers
        /// containing the shard map manager data.
        /// </summary>
        /// <param name="connectionInfo">
        /// Connection info for shard map manager data source.
        /// </param>
        public SqlShardMapManagerCredentials(SqlConnectionInfo connectionInfo)
        {
            ExceptionUtils.DisallowNullArgument(connectionInfo, "connectionInfo");

            // 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(connectionInfo.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",
                connectionInfo.Credential,
                connectionInfo.AccessTokenFactory);

            #endregion GSM Validation

            // Generate connectionInfoShardMapManager
            SqlConnectionStringBuilder connectionStringShardMapManager = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString);

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

            _smmDataSource     = connectionStringShardMapManager.DataSource;
            _smmInitialCatalog = connectionStringShardMapManager.InitialCatalog;

            this.ConnectionInfoShardMapManager =
                connectionInfo.CloneWithUpdatedConnectionString(connectionStringShardMapManager.ConnectionString);

            // Generate connectionInfoShard
            SqlConnectionStringBuilder connectionStringShard = new SqlConnectionStringBuilder(connectionStringBuilder.ConnectionString);

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

            connectionStringShard.ApplicationName = ApplicationNameHelper.AddApplicationNameSuffix(
                connectionStringShard.ApplicationName,
                GlobalConstants.ShardMapManagerInternalConnectionSuffixLocal);

            this.ConnectionInfoShard =
                connectionInfo.CloneWithUpdatedConnectionString(connectionStringShard.ConnectionString);
        }
Esempio n. 2
0
        /// <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="connectionInfo">Input connection info.</param>
        /// <returns>Connection string for DDR connection.</returns>
        private SqlConnectionInfo ValidateAndPrepareConnectionString(
            IShardProvider shardProvider,
            SqlConnectionInfo connectionInfo)
        {
            Debug.Assert(shardProvider != null);
            Debug.Assert(connectionInfo != null);
            Debug.Assert(connectionInfo.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(connectionInfo.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", connectionInfo.Credential);

            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(connectionInfo.CloneWithUpdatedConnectionString(connectionStringBuilder.ConnectionString));
        }