예제 #1
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddCors(c =>
            {
                c.AddPolicy("AllowOrigin", options => options.WithOrigins("http://*****:*****@+";
                options.User.RequireUniqueEmail = false;
            });
            services.AddScoped<DbContext, NoisContext>();
            services.AddScoped<UserManager<User>, UserManager<User>>();
            services.AddScoped<RoleManager<Role>, RoleManager<Role>>();
            services.AddTransient(typeof(IRepositoryAsync<>), typeof(EfRepository<>));
            services.AddTransient(typeof(IUnitOfWork), typeof(UnitOfWork));
            services.AddSingleton(typeof(IRabbitMQService), typeof(RabbitMQService));
            #region Configure for Repositories
            var allRepositoryInterfaces = Assembly.GetAssembly(typeof(IRepositoryAsync<>))
                .GetTypes().Where(t => t.Name.EndsWith("Repository")).ToList();
            var allRepositoryImplements = Assembly.GetAssembly(typeof(EfRepository<>))
                .GetTypes().Where(t => t.Name.EndsWith("Repository")).ToList();

            foreach (var repositoryType in allRepositoryInterfaces.Where(t => t.IsInterface))
            {
                var implement = allRepositoryImplements.FirstOrDefault(c => c.IsClass && repositoryType.Name.Substring(1) == c.Name);
                if (implement != null) services.AddTransient(repositoryType, implement);
            }
            #endregion Configure for Repositories
            #region Configure for Serivces
            var allServicesInterfaces = Assembly.GetAssembly(typeof(IService))
                .GetTypes().Where(t => t.Name.EndsWith("Service")).ToList();
            var allServiceImplements = Assembly.GetAssembly(typeof(Service))
                .GetTypes().Where(t => t.Name.EndsWith("Service")).ToList();

            foreach (var serviceType in allServicesInterfaces.Where(t => t.IsInterface))
            {
                var implement = allServiceImplements.FirstOrDefault(c => c.IsClass && serviceType.Name.Substring(1) == c.Name);
                if (implement != null) services.AddTransient(serviceType, implement);
            }

            #endregion Configure for Serivces

            //services.AddControllers(options =>
            //{
            //    options.Filters.Add<ValidationFilter>();
            //}).AddFluentValidation(x => x.RegisterValidatorsFromAssemblyContaining<Startup>());
            services.AddControllers();
            //Register the Swagger generator
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Version = "v1",
                    Title = "Mentor API",
                    Description = "ASP.NET Core Web API For Mentor Project",
                    TermsOfService = new Uri("https://nois.vn"),
                    Contact = new OpenApiContact
                    {
                        Name = "Mentor",
                        Email = string.Empty,
                        Url = new Uri("https://mentor4u.vn"),
                    },
                    License = new OpenApiLicense
                    {
                        Name = "Use under Mentor",
                        Url = new Uri("https://mentor4u.vn"),
                    }
                });


                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description =
                    "JWT Authorization header using the Bearer scheme. \r\n\r\n Enter 'Bearer' [space] and then your token in the text input below.\r\n\r\nExample: \"Bearer 12345abcdef\"",
                    Name = "Authorization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    Scheme = "Bearer"
                });

                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "Bearer"
                            },
                            Scheme = "Bearer",
                            Name = "Bearer",
                            In = ParameterLocation.Header,

                        },
                        new List<string>()
                    }
                });
            });

            //AutoMapper
            services.AddAutoMapper(typeof(Startup));

            //Hangfire service
            //HangfireService(services);

        }
예제 #2
0
 public EmailService(SendGridConfigure sendGridConfigure)
 {
     _sendGridConfigure = sendGridConfigure;
 }