// This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddControllers(); services.AddCors(options => { options.AddPolicy("CorsPolicy", builder => builder .AllowAnyMethod() .AllowAnyHeader() .AllowAnyOrigin()); }); // If using IIS: services.Configure <IISServerOptions>(options => { options.AllowSynchronousIO = true; }); // If using Kestrel: services.Configure <KestrelServerOptions>(options => { options.AllowSynchronousIO = true; }); if (!Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_CONNECTION_STRING").IsNullOrEmpty()) { services.AddDbContext <InfoRapContext>(options => { options.UseSqlServer(Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_CONNECTION_STRING")); }); } else { services.AddDbContext <InfoRapContext>(options => { options.UseSqlServer(LaunchSettingstUtility.GetEnvironmentVariableFromJSON("ASPNETCORE_ENVIRONMENT_CONNECTION_STRING")); }); } services.AddDbContext <InfoRapContext>(options => { options.UseSqlServer(Configuration.GetConnectionString("EFConnectionString")); }); services.AddControllers().AddNewtonsoftJson(x => x.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore); services.AddAutoMapper(typeof(EntityToDto)); services.AddAutoMapper(typeof(DtoToEntity)); services.Configure <SecurityConfiguration>(opt => { opt.Secret = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_JWT_SECRET"); opt.EmailFrom = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_EMAIL_FROM"); opt.PswFrom = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_EMAIL_SECRET_FROM"); }); services.Configure <InforapMongoConfiguration>(opt => { opt.ConnectionString = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_MONGODB_CONNECTION_STRING"); opt.DatabaseName = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_MONGODB_DATABASENAME"); } ); services.AddDI(); services.AddHostedService <AlertBackgroudService>(); services.AddHostedService <ServicesBackgroudService>(); services.AddHostedService <BackupBackgroudService>(); }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) { Console.WriteLine("-------------------INIT CONTEXT -------------"); //if (!optionsBuilder.IsConfigured) //{ // IConfigurationRoot configuration = new ConfigurationBuilder() // .SetBasePath(Directory.GetCurrentDirectory()) // .AddJsonFile("appsettings.json") // .Build(); // var connectionString = configuration.GetConnectionString("EFConnectionString"); // optionsBuilder.UseSqlServer(connectionString); //} if (!optionsBuilder.IsConfigured) { var conn = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT_CONNECTION_STRING"); if (string.IsNullOrEmpty(conn)) { optionsBuilder.UseSqlServer(LaunchSettingstUtility.GetEnvironmentVariableFromJSON("ASPNETCORE_ENVIRONMENT_CONNECTION_STRING")); } else { optionsBuilder.UseSqlServer(conn); } } }