예제 #1
0
        public override void Execute(WebApplicationBuilder builder, IServiceProvider serviceProvider = null)
        {
            base.Execute(builder, serviceProvider);

            var connections = Options?.Connections;

            if (connections != null && connections.Any())
            {
                var hcBuilder = builder.Services.AddHealthChecks();

                ServiceLifetime lifetime = Options.ServiceLifetime;
                HashSet <Data.DbConnectionSelector> connectionSelectorTable = new();
                var _index = 0;
                foreach (var conn in connections)
                {
                    hcBuilder.AddSqlServer(conn.ConnectionString, name: $"sqlserver-{conn.Name}", tags: new[] { "db", "sql", "sqlserver" });

                    Action <DbContextOptionsBuilder> options = _ =>
                    {
                        _.UseSqlServer(
                            conn.ConnectionString,
                            __ => {
                            __.EnableRetryOnFailure();
                                #warning TODO use new execution strategy options
                        }
                            );
                    };
                    if (_index++ == 0)
                    {
                        builder.Services.AddDbContext <DbContext>(options, lifetime);
                    }

                    connectionSelectorTable.Add(new(conn));
                }

                if (_index > 1)
                {
                    var dbContextCollection = Data.DbConnectionSelector.Collection(builder, connectionSelectorTable);
                    var funcWrapper         = new DbConnectionFunctionWrapper()
                    {
                        Func = type => (Data.DbConnection)dbContextCollection[type.FullName]
                    };
                    builder.Services.AddSingleton(typeof(DbConnectionFunctionWrapper), funcWrapper);
                }


                builder.Services.TryAddTransient(typeof(IRepository <,>), typeof(Repository.EF.SqlServer <,>));
            }
        }
예제 #2
0
        public override void Execute(WebApplicationBuilder builder, IServiceProvider serviceProvider = null)
        {
            base.Execute(builder, serviceProvider);

            var connections = options?.Connections;

            if (connections != null && connections.Any())
            {
                var hcBuilder = builder.Services.AddHealthChecks();

                ServiceLifetime lifetime = ServiceLifetime.Scoped;
                HashSet <Data.DbConnectionSelector> connectionSelectorTable = new();
                var _index = 0;
                foreach (var conn in connections)
                {
                    hcBuilder.AddSqlite(conn.ConnectionString, name: $"sqlite-{conn.Name}", tags: new[] { "db", "sql", "sqlite" });

                    if (_index++ == 0)
                    {
                        builder.Services.AddDbContext <DbContext>(_ => _.UseSqlite(conn.ConnectionString), lifetime);
                    }

                    connectionSelectorTable.Add(new(conn));
                }

                if (_index > 1)
                {
                    var dbContextCollection = Data.DbConnectionSelector.Collection(builder, connectionSelectorTable);
                    var funcWrapper         = new DbConnectionFunctionWrapper()
                    {
                        Func = type => (Data.DbConnection)dbContextCollection[type.FullName]
                    };
                    builder.Services.AddSingleton(typeof(DbConnectionFunctionWrapper), funcWrapper);
                }

                builder.Services.TryAddTransient(typeof(IRepository <,>), typeof(Repository.EF.SQLite <,>));
            }
        }