Example #1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy(
                    CorsPolicyName,
                    builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            services.Configure <TokenConfiguration>(Configuration.GetSection("CheckTokenMiddleware"));
            services.Configure <BaseRabbitMqConfig>(Configuration.GetSection(BaseRabbitMqConfig.SectionName));
            services.Configure <BaseServiceInfoConfig>(Configuration.GetSection(BaseServiceInfoConfig.SectionName));

            services.AddHttpContextAccessor();
            services.AddMemoryCache();
            services.AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            })
            .AddNewtonsoftJson();

            string connStr = Environment.GetEnvironmentVariable("ConnectionString");

            if (string.IsNullOrEmpty(connStr))
            {
                connStr = Configuration.GetConnectionString("SQLConnectionString");

                Log.Information($"SQL connection string from appsettings.json was used. Value '{HidePasswordHelper.HidePassword(connStr)}'.");
            }
            else
            {
                Log.Information($"SQL connection string from environment was used. Value '{HidePasswordHelper.HidePassword(connStr)}'.");
            }

            services.AddDbContext <RightsServiceDbContext>(options =>
            {
                options.UseSqlServer(connStr);
            });

            if (int.TryParse(Environment.GetEnvironmentVariable("MemoryCacheLiveInMinutes"), out int memoryCacheLifetime))
            {
                services.Configure <MemoryCacheConfig>(options =>
                {
                    options.CacheLiveInMinutes = memoryCacheLifetime;
                });
            }
            else
            {
                services.Configure <MemoryCacheConfig>(Configuration.GetSection(MemoryCacheConfig.SectionName));
            }

            if (int.TryParse(Environment.GetEnvironmentVariable("RedisCacheLiveInMinutes"), out int redisCacheLifeTime))
            {
                services.Configure <RedisConfig>(options =>
                {
                    options.CacheLiveInMinutes = redisCacheLifeTime;
                });
            }
            else
            {
                services.Configure <RedisConfig>(Configuration.GetSection(RedisConfig.SectionName));
            }

            services.AddBusinessObjects();
            services.AddTransient <IRedisHelper, RedisHelper>();
            services.AddTransient <ICacheNotebook, CacheNotebook>();

            string redisConnStr = Environment.GetEnvironmentVariable("RedisConnectionString");

            if (string.IsNullOrEmpty(redisConnStr))
            {
                redisConnStr = Configuration.GetConnectionString("Redis");

                Log.Information($"Redis connection string from appsettings.json was used. Value '{HidePasswordHelper.HidePassword(redisConnStr)}'.");
            }
            else
            {
                Log.Information($"Redis connection string from environment was used. Value '{HidePasswordHelper.HidePassword(redisConnStr)}'.");
            }

            services.AddSingleton <IConnectionMultiplexer>(x => ConnectionMultiplexer.Connect(redisConnStr));

            ConfigureMassTransit(services);

            services
            .AddHealthChecks()
            .AddSqlServer(connStr)
            .AddRabbitMqCheck();
        }
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(options =>
            {
                options.AddPolicy(
                    CorsPolicyName,
                    builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyHeader()
                    .AllowAnyMethod();
                });
            });

            services.AddHttpContextAccessor();

            services.Configure <TokenConfiguration>(Configuration.GetSection("CheckTokenMiddleware"));
            services.Configure <BaseServiceInfoConfig>(Configuration.GetSection(BaseServiceInfoConfig.SectionName));
            services.Configure <BaseRabbitMqConfig>(Configuration.GetSection(BaseRabbitMqConfig.SectionName));

            string connStr = Environment.GetEnvironmentVariable("ConnectionString");

            if (string.IsNullOrEmpty(connStr))
            {
                connStr = Configuration.GetConnectionString("SQLConnectionString");

                Log.Information($"SQL connection string from appsettings.json was used. Value '{HidePasswordHelper.HidePassword(connStr)}'.");
            }

            services.AddDbContext <FileServiceDbContext>(options =>
            {
                options.UseSqlServer(connStr);
            });

            services.AddBusinessObjects();

            services
            .AddControllers()
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            })
            .AddNewtonsoftJson();

            services.AddHealthChecks()
            .AddSqlServer(connStr)
            .AddRabbitMqCheck();

            ConfigureMassTransit(services);
        }