/// <summary>
        /// Creates a <see cref="IConnectionMultiplexer"/> using given <see cref="IConnectionStringProvider"/>
        /// </summary>
        public static async Task <IConnectionMultiplexer> CreateAsync(Context context, IConnectionStringProvider connectionStringProvider)
        {
            if (TestConnectionMultiplexer != null)
            {
                return(TestConnectionMultiplexer);
            }

            var connectionStringResult = await connectionStringProvider.GetConnectionString();

            if (!connectionStringResult.Succeeded || string.IsNullOrWhiteSpace(connectionStringResult.ConnectionString))
            {
                var errorMessage =
                    $"Failed to get connection string from provider {connectionStringProvider.GetType().Name}. {connectionStringResult.ErrorMessage}.";
                context.Logger.Error(errorMessage);
                throw new ArgumentException(errorMessage, nameof(connectionStringProvider));
            }

            var connectionString = AllowAdminIfNeeded(connectionStringResult.ConnectionString);
            var options          = ConfigurationOptions.Parse(connectionString);

            options.ClientName         = Environment.MachineName;
            options.AbortOnConnectFail = false;
            options.ConnectTimeout     = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;
            options.SyncTimeout        = (int)TimeSpan.FromSeconds(30).TotalMilliseconds;

            var endpoints = string.Join(", ", options.EndPoints);

            context.Debug($"RedisConnectionMultiplexer: Connecting to Redis endpoint {endpoints}.");

            // Enforce SSL if password is specified. This allows connecting to non password protected local server without SSL
            if (!string.IsNullOrWhiteSpace(options.Password))
            {
                options.Ssl = true;
            }

            var multiplexerTask = Multiplexers.GetOrAdd(
                endpoints,
                _ =>
            {
                return(new Lazy <Task <IConnectionMultiplexer> >(() => GetConnectionMultiplexerAsync(options)));
            });

            return(await multiplexerTask.Value);
        }
Esempio n. 2
0
        public static void Register(IConnectionStringProvider connectionStringProvider)
        {
            if (null == connectionStringProvider)
            {
                throw new ArgumentNullException(nameof(connectionStringProvider));
            }

            ServiceCollection.AddSingleton(typeof(IConnectionStringProvider), connectionStringProvider.GetType());

            ServiceCollection.AddTransient <DbContext, DbContext>();



            MongoClientProxy.SetConnectionString(connectionStringProvider.ConnectionString);

            var _db = MongoAdmin.GetDatabase(MongoClientProxy.Instance, connectionStringProvider.DatabaseName);

            InitializeMigration(_db);
        }