public IServiceProvider ConfigureServices(IServiceCollection services) { services.AddMvc(); #region Database configuration // Adding DbContext For Sample and Sample 2 using the same database services.AddDbContext <SampleDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Sample")).EnableSensitiveDataLogging(), ServiceLifetime.Transient); services.AddDbContext <Sample2DbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Sample")).EnableSensitiveDataLogging(), ServiceLifetime.Transient); // Adding DbContext For Test in another database services.AddDbContext <TestDbContext>(options => options.UseSqlServer(Configuration.GetConnectionString("Test")).EnableSensitiveDataLogging(), ServiceLifetime.Transient); // Adding all dbcontext as a service retrieved for DbContext so we can call all of them as IEnumerable<DbContext> services.AddScoped <DbContext>(provider => provider.GetService <SampleDbContext>()); services.AddScoped <DbContext>(provider => provider.GetService <Sample2DbContext>()); services.AddScoped <DbContext>(provider => provider.GetService <TestDbContext>()); // Scanning our assembly and adding all ISeed SeedScanner.FindSeedersInAssembly(Assembly.GetExecutingAssembly()) .ForEach(d => services.AddScoped(d.InterfaceType, d.ImplementationType)); // Adding our EfCoreSeeder service services.AddScoped <ISeeder, EfCoreSeeder>(); #endregion return(services.BuildServiceProvider()); }
public void ConfigureServices(IServiceCollection services) { var apiAssembly = Assembly.GetExecutingAssembly(); #region Database //DbContext services.AddDbContext <ApiDbContext>(options => options .EnableDetailedErrors(Environment.IsDevelopment()) .EnableSensitiveDataLogging(Environment.IsDevelopment()) .UseInMemoryDatabase("PetShopDB")); //Seeder services.AddScoped <DbContext>(provider => provider.GetService <ApiDbContext>()); SeedScanner.FindSeedersInAssembly(apiAssembly) .ForEach(d => services.AddScoped(d.InterfaceType, d.ImplementationType)); services.AddScoped <ISeeder, EfCoreSeeder>(); #endregion #region MVC services.AddControllers(config => { config.Filters.Add(typeof(NotificationFilter)); }).AddJsonOptions(config => { config.JsonSerializerOptions.IgnoreNullValues = true; config.JsonSerializerOptions.PropertyNameCaseInsensitive = true; }); #endregion #region Authorization services.AddAuthentication((options) => { options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme; options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme; }).AddJwtBearer(options => { options.RequireHttpsMetadata = false; options.SaveToken = true; options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuerSigningKey = true, IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Auth:Key"])), ValidateAudience = true, ValidAudience = Configuration["Auth:Audience"], ValidateIssuer = true, ValidIssuer = Configuration["Auth:Issuer"], ValidateLifetime = true, ClockSkew = TimeSpan.Zero }; }); #endregion #region Infrastructure services.AddScoped <NotificationContext>(); services.AddMediatR(apiAssembly); services.AddTransient(_ => Configuration); AssemblyScanner.FindValidatorsInAssembly(apiAssembly) .ForEach(validator => services.AddScoped(validator.InterfaceType, validator.ValidatorType)); services.AddScoped(typeof(IPipelineBehavior <,>), typeof(NotificationValidationBehavior <,>)); #endregion #region Swagger services.AddSwaggerGen(config => { config.SwaggerDoc("v1", new OpenApiInfo { Title = "Franca API", Version = "v1", }); config.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme { Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"", Name = "Authorization", }); config.DocumentFilter <LowercaseDocumentFilter>(); }); services.ConfigureSwaggerGen(c => c.CustomSchemaIds(x => x.FullName)); #endregion }