Exemple #1
0
        private static void RunMassTransitReceiverWithRabbitMq()
        {
            var         rabbitConfig     = RabbitConfigurationsLoader.LoadConfigurations(appsettingsFileName, true);
            IBusControl rabbitBusControl = Bus.Factory.CreateUsingRabbitMq(rabbit =>
            {
                var host = rabbit.Host(rabbitConfig.URL, "/", settings =>
                {
                    settings.Password(rabbitConfig.Password);
                    settings.Username(rabbitConfig.Username);
                });

                rabbit.ReceiveEndpoint(host, rabbitConfig.QueueName, conf =>
                {
                    conf.PrefetchCount      = rabbitConfig.MaxConcurrentMessages;
                    conf.ExchangeType       = rabbitConfig.ExchangeType;
                    conf.Durable            = rabbitConfig.Durable;
                    conf.AutoDelete         = rabbitConfig.AutoDelete;
                    conf.DeadLetterExchange = rabbitConfig.DeadLetterExchange;
                    conf.BindDeadLetterQueue(rabbitConfig.DeadLetterExchange, rabbitConfig.DeadLetterQueueName);
                    conf.UseMessageRetry(r => r.Interval(rabbitConfig.FailRetryCount, rabbitConfig.FailRetryInterval));
                    conf.Consumer <NotificationsConsumeHandler>();
                });
            });

            rabbitBusControl.Start();
            Console.ReadKey();
            rabbitBusControl.Stop();
        }
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            #region Masstransit
            services.AddScoped <NotificationsConsumeHandler>();
            services.AddMassTransit(c =>
            {
                c.AddConsumer <NotificationsConsumeHandler>();
            });
            var rabbitConfig = RabbitConfigurationsLoader.LoadConfigurations("appsettings.json");
            services.AddSingleton(provider => Bus.Factory.CreateUsingRabbitMq(
                                      cfg =>
            {
                var host = cfg.Host(rabbitConfig.URL, "/", settings => {
                    settings.Password(rabbitConfig.Password);
                    settings.Username(rabbitConfig.Username);
                });
            }));
            services.AddSingleton <IBus>(provider => provider.GetRequiredService <IBusControl>());
            services.AddSingleton <IHostedService, BusService>();

            #endregion
        }
Exemple #3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc()
            .AddFluentValidation(fvc =>
            {
                fvc.RegisterValidatorsFromAssemblyContaining <Startup>();
                fvc.ImplicitlyValidateChildProperties = true;
                fvc.RunDefaultMvcValidationAfterFluentValidationExecutes = true;
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.SuppressModelStateInvalidFilter = true;
            });

            SystemConstants.ConnectionString = Configuration.GetConnectionString("DbConnection");
            services.AddDbContext <ELMCustomersDbContext>(options =>
                                                          options.UseSqlServer(SystemConstants.ConnectionString));

            services.TryAddScoped(typeof(ELMCustomersDbContext), typeof(ELMCustomersDbContext));
            services.TryAddScoped(typeof(DbContext), typeof(ELMCustomersDbContext));
            services.TryAddScoped(typeof(IGenericRepository <>), typeof(GenericRepository <>));
            services.TryAddScoped(typeof(ICustomerService), typeof(CustomerService));
            //services.AddTransient<IValidator<CustomerDTO>, CreateCustomerValidator>();
            services.AddTransient <IValidator <List <CustomerDTO> >, CreateCustomerListValidator>();

            // Register the Swagger generator, defining 1 or more Swagger documents
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Customers API", Version = "v1"
                });
            });

            services.AddLogging(builder =>
            {
                builder.AddConfiguration(Configuration.GetSection("Logging"))
                .AddSerilog(new LoggerConfiguration().WriteTo.File($"{Configuration.GetSection("Logging").GetSection("Path").Value}").CreateLogger())
                .AddConsole();
#if DEBUG
                builder.AddDebug();
#endif
            });

            #region Masstransit
            services.AddScoped <CustomersConsumeHandler>();
            services.AddMassTransit(c =>
            {
                c.AddConsumer <CustomersConsumeHandler>();
            });
            var rabbitConfig = RabbitConfigurationsLoader.LoadConfigurations("appsettings.json");
            services.AddSingleton(provider => Bus.Factory.CreateUsingRabbitMq(
                                      cfg =>
            {
                var host = cfg.Host(rabbitConfig.URL, "/", settings => {
                    settings.Password(rabbitConfig.Password);
                    settings.Username(rabbitConfig.Username);
                });
            }));
            services.AddSingleton <IBus>(provider => provider.GetRequiredService <IBusControl>());
            services.AddSingleton <IHostedService, BusService>();

            #endregion
        }