Esempio n. 1
0
 public static void AddStaticHelper(this IServiceCollection services)
 {
     DependencyInjectionHelper.Init(services);
     EngineerContext.Init(services.BuildServiceProvider());
 }
Esempio 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.AddControllers();

            ApiHelper.Init(Configuration);
            WebHelper.Init(ApiHelper.GetServer());
            JwtTokenHelper.Init(Configuration);

            //services.AddMvc(options => {
            //    options.Filters.Add(typeof(ExceptionFilter));
            //    options.OutputFormatters.Insert(0, new CustomOutputFormater());
            //}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

            services.AddDbContext <AQConfigContext>(options =>
                                                    options.UseSqlServer(ApiHelper.GetConnectionStringConfig()));

            services.AddDbContext <AQYachtContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringYacht()));

            services.AddDbContext <AQDiningContext>(options =>
                                                    options.UseSqlServer(ApiHelper.GetConnectionStringDining()));

            services.AddDbContext <AQEvisaContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringEvisa()));

            services.AddDbContext <AQHotelContext>(options =>
                                                   options.UseSqlServer(ApiHelper.GetConnectionStringHotel()));

            services.AddDbContext <AQCMSContext>(options =>
                                                 options.UseSqlServer(ApiHelper.GetConnectionStringCMS()));

            ServiceHelper.Init(services.BuildServiceProvider());

            services.AddSwaggerGen(c =>
            {
                //.Netcore 2.2

                /*c.SwaggerDoc("v1", new OpenApiInfo { Title = "Admin Api", Version = "v1" });
                 *
                 * c.AddSecurityDefinition(JwtBearerDefaults.AuthenticationScheme, new ApiKeyScheme
                 * {
                 *  Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                 *  Name = "Authorization",
                 *  In = "header",
                 *  Type = "apiKey"
                 * });
                 * c.AddSecurityRequirement(new Dictionary<string, IEnumerable<string>>
                 * {
                 * { JwtBearerDefaults.AuthenticationScheme, new string[] { } },
                 * });
                 *
                 * var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                 * var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                 * c.IncludeXmlComments(xmlPath);
                 * c.DescribeAllEnumsAsStrings();*/


                //Netcore 3.1
                c.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "Admin API", Version = "v1"
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Name         = "Authorization",
                    Type         = SecuritySchemeType.ApiKey,
                    Scheme       = "Bearer",
                    BearerFormat = "JWT",
                    In           = ParameterLocation.Header,
                    Description  = "JWT Authorization header using the Bearer scheme."
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id   = "Bearer"
                            }
                        },
                        new string[] {}
                    }
                });
            });
            services.AddAuthentication()
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = JwtTokenHelper.GetValidationParameters();
                options.SaveToken            = true;
                options.RequireHttpsMetadata = false;
            });

            // Init & register Automapper
            services.AddAutoMapper(typeof(AutoMapperConfig));
            services.AddHttpContextAccessor();
            RegisterDependencyInjection(services);
            EngineerContext.Init(services.BuildServiceProvider());
        }