public RabbitMQMessageSender(string exchangeName
                                     , string routeKey
                                     , RabbitMQChannelPool rabbitMQChannelPool
                                     , IMessageExchangeSerializer serializer)
        {
            _routeKey     = routeKey;
            _serializer   = serializer;
            _exchangeName = exchangeName;

            _rabbitMQChannelPool = rabbitMQChannelPool;

            CreateExchangeAndQueueIfNotExist();
        }
Ejemplo n.º 2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <DeliveryContext>(options =>
                                                    options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")).UseSnakeCaseNamingConvention());

            services.Configure <WarehouseRabbitMQOption>(Configuration.GetSection("RabbitMq"));

            services.AddSingleton(provider => {
                return(new MapperConfiguration(cfg =>
                {
                    cfg.Advanced.AllowAdditiveTypeMapCreation = true;
                    cfg.AddProfile(new AutoMapperProfile());
                }).CreateMapper());
            });

            services.AddScoped <Services.DeliveryService>();
            services.AddSingleton((sp) =>
            {
                var rabbitMQOption = sp.GetService <IOptions <WarehouseRabbitMQOption> >()?.Value;
                var chPool         = new RabbitMQChannelPool(new RabbitMqConnectionPool(rabbitMQOption.ConnectionString));
                new RabbitMQMessageSender(rabbitMQOption.ExchangeName
                                          , rabbitMQOption.WarehouseQueueName
                                          , chPool
                                          , new JsonNetMessageExchangeSerializer());

                return(new RabbitMQMessageSender(rabbitMQOption.ExchangeName
                                                 , rabbitMQOption.QueueName
                                                 , chPool
                                                 , new JsonNetMessageExchangeSerializer()));
            });

            services.AddRabbitMQConsumer();

            services.AddControllers();
            services.AddHealthChecks();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "OTUS.HomeWork.DeliveryService", Version = "v1"
                });
            });
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddDbContext <WarehouseContext>(options =>
                                                     options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")).UseSnakeCaseNamingConvention());

            services.Configure <WarehouseRabbitMQOption>(Configuration.GetSection("RabbitMq"));
            services.Configure <RedisOption>(Configuration.GetSection("Redis"));
            services.AddScoped <ProductRepository>();
            services.AddScoped <Services.WarehouseService>();
            services.AddHttpClient();

            var deliveryOptionsSection = Configuration.GetSection("DeliveryService");

            services.AddScoped((sp) =>
            {
                var options = deliveryOptionsSection.Get <ServiceAddressOption>();
                var client  = sp.GetService <IHttpClientFactory>()?.CreateClient("DeliveryService");
                return(new DeliveryServiceClient(options.Url, client));
            });

            var billingSettingSection = Configuration.GetSection("EshopService");

            services.AddScoped((sp) =>
            {
                var options = billingSettingSection.Get <ServiceAddressOption>();
                var client  = sp.GetService <IHttpClientFactory>()?.CreateClient("EshopService");
                return(new EshopServiceClient(options.Url, client));
            });


            services.AddSingleton(provider => {
                return(new MapperConfiguration(cfg =>
                {
                    cfg.Advanced.AllowAdditiveTypeMapCreation = true;
                    cfg.AddProfile(new AutoMapperProfile());
                }).CreateMapper());
            });

            services.AddSingleton((sp) =>
            {
                var rabbitMQOption = sp.GetService <IOptions <WarehouseRabbitMQOption> >()?.Value;
                var chPool         = new RabbitMQChannelPool(new RabbitMqConnectionPool(rabbitMQOption.ConnectionString));
                new RabbitMQMessageSender(rabbitMQOption.ExchangeName
                                          , rabbitMQOption.DeliveryRouteKey
                                          , chPool
                                          , new JsonNetMessageExchangeSerializer());

                new RabbitMQMessageSender(rabbitMQOption.ExchangeName
                                          , rabbitMQOption.WarehouseRouteKey
                                          , chPool
                                          , new JsonNetMessageExchangeSerializer());

                return(new RabbitMQMessageSender(rabbitMQOption.ExchangeName
                                                 , rabbitMQOption.EshopNotificationRouteKey
                                                 , chPool
                                                 , new JsonNetMessageExchangeSerializer()));
            });

            services.AddHangfire(config =>
                                 config.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                                 .UseSimpleAssemblyNameTypeSerializer()
                                 .UseDefaultTypeSerializer()
                                 .UseMemoryStorage());
            services.AddHangfireServer();

            services.AddRabbitMQConsumer();

            var redisOption = Configuration.GetSection("Redis").Get <RedisOption>();

            services.AddStackExchangeRedisCache(options =>
            {
                options.Configuration = redisOption.Url;
                options.InstanceName  = "Instance-1";
            });

            services.AddAuthentication(g =>
            {
                g.DefaultAuthenticateScheme = SimpleCustomAuthenticationHandler.AuthentificationScheme;
                g.DefaultChallengeScheme    = SimpleCustomAuthenticationHandler.AuthentificationScheme;
                g.DefaultForbidScheme       = SimpleCustomAuthenticationHandler.AuthentificationScheme;
            }).AddScheme <RestAPIAuthOption, SimpleCustomAuthenticationHandler>(SimpleCustomAuthenticationHandler.AuthentificationScheme, o => { });

            services.Configure <ScheduleJobsOption>(Configuration.GetSection("ScheduleJobs"));
            services.AddTransient <ReserveProductTrackerJob>();
            services.AddHostedService <HangfireJobScheduler>();

            services.AddControllers();
            services.AddHealthChecks();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "OTUS.HomeWork.WarehouseService", Version = "v1"
                });
            });

            services.AddProblemDetails();
        }