Esempio n. 1
0
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <FormOptions>(x =>
            {
                x.ValueLengthLimit            = int.MaxValue;
                x.MultipartBodyLengthLimit    = int.MaxValue;
                x.MultipartHeadersLengthLimit = int.MaxValue;
            });

            services.AddOptions();
            services.Configure <AtanetSettings>(this.Configuration.GetSection("AtanetSettings"));
            services.AddSingleton(this.Configuration);
            services.AddSingleton(x => x.GetService <IOptions <AtanetSettings> >().Value);
            services.AddSingleton <IApiResultService, ApiResultService>();

            services.AddCors(x => x.AddDefaultPolicy(builder => builder
                                                     .AllowAnyHeader()
                                                     .AllowAnyMethod()
                                                     .AllowAnyOrigin()
                                                     .AllowCredentials()));

            services.AddAutoMapper(x => x.AddProfiles(this.GetAssemblies().ToList()));
            var mvc = services.AddMvc(config =>
            {
                config.Filters.Add(typeof(ValidateActionFilter));
            });

            mvc.AddFluentValidation(fv =>
            {
                fv.ValidatorFactoryType = typeof(ValidationService);
                foreach (var assembly in AssemblyUtilities.GetAtanetAssemblies())
                {
                    fv.RegisterValidatorsFromAssembly(assembly);
                }
            });

            var apiResultServiceInstance = services.BuildServiceProvider().GetService <IApiResultService>();

            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultScheme             = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme    = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(o =>
            {
                o.SecurityTokenValidators.Clear();
                o.SecurityTokenValidators.Add(new GoogleTokenValidator(apiResultServiceInstance));
            });

            services.AddTransient <IValidatorFactory, ValidationService>();
            mvc.AddMvcOptions(o => o.Filters.Add(typeof(GlobalExceptionFilter)));
            services.AddSwaggerGen(x => x.OperationFilter <SwaggerFilter>());
            var connectionString = new ConnectionStringBuilder().ConstructConnectionStringFromEnvironment();

            services.AddDbContext <AtanetDbContext>(options =>
            {
                options.UseMySql(connectionString);
            });

            services.AddSingleton <IUnitOfWorkFactory, UnitOfWorkFactory>();
            this.ConfigureBusinessRules(services);
            services.AddScoped <IPostCreationService, PostCreationService>();
            services.AddScoped <IPostFilterService, PostFilterService>();
            services.AddScoped <ICommentCreationService, CommentCreationService>();
            services.AddScoped <ICommentFilterService, CommentFilterService>();
            services.AddScoped <IFileCreationService, FileCreationService>();
            services.AddScoped <IUserService, UserService>();
            services.AddScoped <IPictureService, PictureService>();
            services.AddScoped <IQueryService, QueryService>();
            services.AddScoped <IScoreService, ScoreService>();
            services.AddScoped <IPostReactionCreationService, PostReactionCreationService>();
            services.AddScoped <ISentimentService, SentimentService>();
            services.TryAddSingleton <IHttpContextAccessor, HttpContextAccessor>();
            services.AddSingleton <IAssemblyContainer>(x => new AssemblyContainer(this.GetAssemblies()));
            services.AddSingleton <IPagingValidator, PagingValidator>();
            services.AddSingleton <IBusinessRuleRegistry, BaseBusinessRuleRegistry>();
            services.AddSingleton <IConnectionStringBuilder, ConnectionStringBuilder>();

            var context = services.BuildServiceProvider().GetService <AtanetDbContext>();

            context.Database.EnsureCreated();

            // This instance needs to be created for the compiler to reference the Atanet.Validation assembly
            var instance = new PagedPostDtoValidator();
        }
Esempio n. 2
0
 private IEnumerable <Assembly> GetAssemblies() =>
 AssemblyUtilities.GetAtanetAssemblies();