Exemplo n.º 1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddResponseCompression();
            if (_env.IsDevelopment())
            {
                services.AddDbContext <VehiclesPriceListAppContext>(
                    opt => opt   // .UseLazyLoadingProxies()
                    .UseSqlite(_conf.GetConnectionString("SqliteConnection"), b => b.MigrationsAssembly("VehiclesPriceListRestApi")));
            }
            else if (_env.IsProduction())
            {
                services.AddDbContext <VehiclesPriceListAppContext>(
                    opt => opt   // .UseLazyLoadingProxies()
                    .UseSqlServer(_conf.GetConnectionString("DefaultConnection")));
            }
            var mappingConfig = new MapperConfiguration(mc =>
            {
                mc.AddProfile(new SimpleMappings());
            });

            IMapper mapper = mappingConfig.CreateMapper();

            services.AddSingleton(mapper);

            #region toremove
            // ===== Add Jwt Authentication ========

            //services
            //    .AddAuthentication(options =>
            //    {
            //        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            //        options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            //        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;

            //    })
            //    .AddJwtBearer(cfg =>
            //    {
            //        cfg.RequireHttpsMetadata = false;
            //        cfg.SaveToken = true;
            //        cfg.TokenValidationParameters = new TokenValidationParameters
            //        {
            //            ValidIssuer = _conf["JwtIssuer"],
            //            ValidAudience = _conf["JwtIssuer"],
            //            IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_conf["JwtKey"])),
            //            ClockSkew = TimeSpan.Zero // remove delay of token when expire
            //        };
            //    });
            #endregion

            ServicesConfiguration.ConfigureDomainService(services);

            ServicesConfiguration.ConfigureLoggerService(services);

            //side and the client side projects will run on different domains
            //enable CORS on the server side project
            services.AddCors(options =>
            {
                options.AddPolicy("CorsPolicy",
                                  builder =>
                                  builder.WithOrigins("http://localhost:4200")
                                  .AllowAnyMethod()
                                  .AllowAnyHeader()
                                  .AllowCredentials());
            });

            services.AddControllers().AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
                var resolver = options.SerializerSettings.ContractResolver;
                if (resolver != null)
                {
                    (resolver as DefaultContractResolver).NamingStrategy = null;
                }
            });

            services
            .AddControllersWithViews()
            .AddFluentValidation(fvc => fvc.RegisterValidatorsFromAssemblyContaining <ValidatorsCollection>())
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Awesome API", Version = "v1"
                });
            });
        }