Beispiel #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            AppDbConfig dbConfig = new AppDbConfig();

            services.AddDbContext <AppDbContext>(opt => opt.UseInMemoryDatabase(dbConfig.DBName));
            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Hahn API", Version = "v1"
                });
                c.ExampleFilters();
            });
            services.AddSwaggerExamplesFromAssemblyOf <Applicant>();
            services.AddCors(o => o.AddPolicy("MyPolicy", builder =>
            {
                builder.AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader();
            }));
        }
 public AccountModuleMigrationRunner(AppDbConfig appDbConfig)
 {
     AppDbConfig          = appDbConfig;
     VersionTableMetaData = new _0001_VersionTable();
     Assemblies           = new[] { typeof(_0001_VersionTable).Assembly };
 }
        public void Register(IServiceCollection services, IConfiguration configuration)
        {
            var         connectionString = configuration.GetConnectionString("SqlServerConnectionStr");
            AppDbConfig appDbConfig      = new AppDbConfig(connectionString);

            services.AddSingleton(appDbConfig);
            services.AddDbContext <EfAppDbContext>((provider, builder) =>
            {
                builder.UseLoggerFactory(provider.GetService <ILoggerFactory>())
                .EnableSensitiveDataLogging();

                builder.UseSqlServer(provider.GetRequiredService <AppDbConfig>().ConnectionStr);
            });
            services.AddDomainMessageBroker(GetType().Assembly.GetReferencedAssemblies().Select(Assembly.Load).ToArray());
            services.AddScoped <IExecutionContext, AppExecutionContext>();

            services.AddMessageStorage(messageStorage =>
            {
                messageStorage.UseSqlServer(appDbConfig.ConnectionStr)
                .UseHandlers((handlerManager, provider) =>
                {
                    handlerManager.TryAddHandler(new HandlerDescription <IntegrationEventHandler>
                                                     (() =>
                    {
                        var x = provider.GetRequiredService <IBusControl>();
                        return(new IntegrationEventHandler(x));
                    })
                                                 );

                    handlerManager.TryAddHandler(new HandlerDescription <IntegrationCommandHandler>
                                                     (() =>
                    {
                        var x = provider.GetRequiredService <IBusControl>();
                        return(new IntegrationCommandHandler(x));
                    })
                                                 );
                });
            })
            .WithJobProcessor();
            services.AddMessageStorageHostedService();
            services.AddScoped <IOutboxClient, OutboxClient>();

            var massTransitBusConfiguration = configuration.GetSection("MassTransitConfiguration")
                                              .Get <MassTransitBusConfiguration>();

            services.AddSingleton(massTransitBusConfiguration);
            services.AddMassTransitHostedService();
            services.AddMassTransit(serviceCollectionBusConfigurator =>
            {
                using (ServiceProvider serviceProvider = serviceCollectionBusConfigurator.Collection.BuildServiceProvider())
                {
                    using (IServiceScope serviceScope = serviceProvider.CreateScope())
                    {
                        IEnumerable <IIntegrationMessageConsumerAssembly> consumerRegistrars = serviceScope.ServiceProvider.GetServices <IIntegrationMessageConsumerAssembly>();
                        foreach (var integrationMessageConsumerRegistrar in consumerRegistrars)
                        {
                            serviceCollectionBusConfigurator.AddConsumers(integrationMessageConsumerRegistrar.Assembly);
                        }
                    }
                }

                serviceCollectionBusConfigurator.AddBus(provider =>
                {
                    var busConfiguration = provider.GetRequiredService <MassTransitBusConfiguration>();
                    var busControl       =
                        Bus.Factory.CreateUsingRabbitMq(cfg =>
                    {
                        cfg.Host($"{busConfiguration.HostName}",
                                 busConfiguration.VirtualHost,
                                 hst =>
                        {
                            hst.Username(busConfiguration.UserName);
                            hst.Password(busConfiguration.Password);
                            hst.UseCluster(clusterConfigurator => { clusterConfigurator.Node($"{busConfiguration.HostName}:{busConfiguration.Port}"); });
                        });
                        cfg.ConfigureJsonSerializer(settings =>
                        {
                            settings.Converters.Add(new MassTransitTypeNameHandlingConverter(TypeNameHandling.Auto));
                            return(settings);
                        });
                        cfg.ConfigureJsonDeserializer(settings =>
                        {
                            settings.Converters.Add(new MassTransitTypeNameHandlingConverter(TypeNameHandling.Auto));
                            return(settings);
                        }
                                                      );

                        cfg.ConfigureEndpoints(provider);
                        cfg.UseRetry(cfg, configurator => configurator.Interval(3, TimeSpan.FromSeconds(3)));
                    });

                    return(busControl);
                });
            });
        }