Пример #1
0
        public static IHost BuildHost(
            DatabaseType databaseType,
            RelationLocatorStrategy strategy,
            IEnumerable <string> configJsonFilenames,
            Action <ContainerBuilder> builderAction = null,
            Action <IClaptrapBootstrapperBuilder> bootstrapperAction = null)
        {
            var hostBuilder = new HostBuilder();

            hostBuilder
            .ConfigureServices(collection =>
            {
                collection.AddLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                    logging.AddNLog();
                });
            })
            .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder
                .AddJsonFile("configs/appsettings.json")
                .AddJsonFile($"configs/db_configs/claptrap.{databaseType:G}.json".ToLower())
                .AddJsonFile($"configs/db_configs/claptrap.{databaseType:G}.{strategy:G}.json".ToLower());
                foreach (var filename in configJsonFilenames)
                {
                    configurationBuilder.AddJsonFile(filename);
                }

                configurationBuilder.AddEnvironmentVariables();
            })
            .UseClaptrap(bootstrapperBuilder =>
            {
                bootstrapperBuilder.ScanClaptrapDesigns(new[]
                {
                    typeof(IAccount),
                    typeof(Account),
                    typeof(IAccountBalanceMinion),
                    typeof(AccountBalanceMinion),
                    typeof(IAccountHistoryBalanceMinion),
                    typeof(AccountHistoryBalanceMinion)
                });
                bootstrapperBuilder.ConfigureClaptrapDesign(x =>
                                                            x.ClaptrapOptions.EventCenterOptions.EventCenterType = EventCenterType.None);
                bootstrapperAction?.Invoke(bootstrapperBuilder);
            }, containerBuilder =>
            {
                builderAction?.Invoke(containerBuilder);
                containerBuilder.RegisterType <Account>()
                .AsSelf()
                .InstancePerDependency();
                containerBuilder.RegisterType <AccountBalanceMinion>()
                .AsSelf()
                .InstancePerDependency();
            });
            var host = hostBuilder.Build();

            return(host);
        }
Пример #2
0
 protected QuickSetupTestBase(
     DatabaseType databaseType,
     RelationLocatorStrategy strategy)
 {
     DatabaseType = databaseType;
     Strategy     = strategy;
     // ReSharper disable once VirtualMemberCallInConstructor
     Init();
 }
Пример #3
0
        static async Task Main(string[] args)
        {
            var configBuilder = new ConfigurationBuilder();
            var config        = configBuilder.AddJsonFile(Path.Combine("configs", "appsettings.json"))
                                .AddEnvironmentVariables()
                                .AddCommandLine(args)
                                .Build();

            var options = new TestConsoleOptions();

            config.Bind(nameof(TestConsoleOptions), options);

            const RelationLocatorStrategy strategy = RelationLocatorStrategy.SharedTable;
            var host = QuickSetupTestHelper.BuildHost(options.DatabaseType,
                                                      strategy,
                                                      Enumerable.Empty <string>(),
                                                      containerBuilder =>
            {
                containerBuilder.RegisterModule <StorageTestConsoleModule>();
                containerBuilder.RegisterModule <StorageSetupModule>();
            }, services => { },
                                                      hostBuilder =>
            {
                hostBuilder.ConfigureServices(services =>
                {
                    services.AddOptions <TestConsoleOptions>()
                    .Configure(consoleOptions => config.Bind(nameof(TestConsoleOptions), consoleOptions));
                });
            });
            var serviceProvider = host.Services;

            await using var scope = serviceProvider.GetService <ILifetimeScope>();
            var logger = scope !.Resolve <ILogger <Program> >();

            try
            {
                var   service = scope.ResolveKeyed <ITestJob>(options.Job);
                await service !.RunAsync();
            }
            catch (Exception e)
            {
                logger.LogError(e, "error while storage test");
            }
        }
        public static IHost BuildHost(
            DatabaseType databaseType,
            RelationLocatorStrategy strategy,
            IEnumerable <string> configJsonFilenames,
            Action <ContainerBuilder> builderAction = null,
            Action <IClaptrapBootstrapperBuilder> bootstrapperAction = null,
            Action <HostBuilder> configureHostBuilder = null)
        {
            var hostBuilder = new HostBuilder();

            hostBuilder
            .ConfigureAppConfiguration(configurationBuilder =>
            {
                configurationBuilder
                .AddJsonFile("configs/appsettings.json")
                .AddJsonFile($"configs/db_configs/claptrap.{databaseType:G}.json".ToLower())
                .AddJsonFile($"configs/db_configs/claptrap.{databaseType:G}.{strategy:G}.json".ToLower());
                foreach (var filename in configJsonFilenames)
                {
                    configurationBuilder.AddJsonFile(filename);
                }

                configurationBuilder.AddEnvironmentVariables();
            })
            .ConfigureServices(collection =>
            {
                collection.AddLogging(logging =>
                {
                    logging.ClearProviders();
                    logging.SetMinimumLevel(LogLevel.Trace);
                    logging.AddNLog();
                });
            })
            .UseServiceProviderFactory(context =>
            {
                return(new AutofacServiceProviderFactory(builder =>
                {
                    var loggerFactory = new ServiceCollection()
                                        .AddLogging(logging =>
                    {
                        logging.SetMinimumLevel(LogLevel.Trace);
                        logging.AddConsole();
                    })
                                        .BuildServiceProvider()
                                        .GetRequiredService <ILoggerFactory>();
                    var claptrapBootstrapperBuilder = new AutofacClaptrapBootstrapperBuilder(loggerFactory)
                                                      .ScanClaptrapModule()
                                                      .AddConfiguration(context.Configuration)
                                                      .ScanClaptrapDesigns(new[]
                    {
                        typeof(IAccount),
                        typeof(Account),
                        typeof(IAccountBalanceMinion),
                        typeof(AccountBalanceMinion),
                        typeof(IAccountHistoryBalanceMinion),
                        typeof(AccountHistoryBalanceMinion),
                        typeof(ICustomFactoryClaptrap),
                        typeof(CustomFactoryClaptrap)
                    })
                                                      .ConfigureClaptrapDesign(x =>
                                                                               x.ClaptrapOptions.EventCenterOptions.EventCenterType = EventCenterType.None);
                    bootstrapperAction?.Invoke(claptrapBootstrapperBuilder);
                    var claptrapBootstrapper =
                        (AutofacClaptrapBootstrapper)claptrapBootstrapperBuilder
                        .Build();
                    claptrapBootstrapper.Boot(builder);

                    builder.RegisterType <Account>()
                    .AsSelf()
                    .InstancePerDependency();
                    builder.RegisterType <AccountBalanceMinion>()
                    .AsSelf()
                    .InstancePerDependency();
                    builder.RegisterType <CustomFactoryClaptrap>()
                    .AsSelf()
                    .InstancePerDependency();

                    builder.RegisterModule <CustomLoaderAndSaverModule>();

                    builderAction?.Invoke(builder);
                }));
            })
            .ConfigureServices((_, collection) => { collection.AddClaptrapServerOptions(); });
            configureHostBuilder?.Invoke(hostBuilder);
            var host = hostBuilder.Build();

            return(host);
        }