Ejemplo n.º 1
0
        /// <summary>
        /// Configures the service container.
        /// </summary>
        /// <param name="services">The service container.</param>
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMemoryCache();

            // Enabled CORS to allow access from browser applications on different domains.
            services.AddCors();

            // Configure the API and JSON behaviour.
            services.AddMvc(options =>
            {
                options.InputFormatters.RemoveType <JsonPatchInputFormatter>();
                options.OutputFormatters.RemoveType <StringOutputFormatter>();
                options.Filters.Add(new ParameterValidationFilter());
                options.Filters.Add(new ModelStateValidationFilter());
                options.Filters.Add(new EntityNotFoundExceptionFilter());
                options.Filters.Add(new InvalidTradeExceptionFilter());
                options.Filters.Add(new ValidationExceptionFilter());
            })
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
                options.SerializerSettings.Converters.Add(new OnlyDateConverter());
                options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
            });

            // Configure JWT authentication.
            services.AddAuthentication(options =>
            {
                options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                options.TokenValidationParameters = new TokenValidationParameters
                {
                    ValidateIssuerSigningKey = true,
                    IssuerSigningKey         = JwtSettings.SecurtyKey,
                    ValidIssuer    = JwtSettings.Issuer,
                    ValidAudiences = new[] { JwtSettings.InvestorAudience, JwtSettings.AdministratorAudience }
                };
            });

            // Configure Authorization to prevent investor users from using admin features.
            services.AddAuthorization(options =>
            {
                options.AddPolicy(
                    AuthorizationPolicies.Administrators,
                    policy => policy.RequireClaim(JwtRegisteredClaimNames.Aud, JwtSettings.AdministratorAudience));
            });

            // Enable Swagger and Swagger UI to make exploration of the API easier.
            services.AddSwaggerGen(SwaggerConfig.Configure);

            // Register the components from all modules in the dependency injection container.
            DomainModule.ConfigureServices(services);
            RepositoriesModule.ConfigureServices(services);
            AsxModule.ConfigureServices(services);
            YahooModule.ConfigureServices(services);
        }
Ejemplo n.º 2
0
        private void Init()
        {
            _servicesCollection = new ServiceCollection();

            // mock with memory database
            _servicesCollection.AddDbContext <MyExpensesContext>(options => options.UseInMemoryDatabase(Guid.NewGuid().ToString()));

            // configure all others projects
            DomainModule.ConfigureServices(_servicesCollection);
            InfrastructureModule.ConfigureServices(_servicesCollection);

            // build
            _serviceProvider = _servicesCollection.BuildServiceProvider();
        }
Ejemplo n.º 3
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers(options =>
                                    options.Filters.Add(new HttpResponseUserFriendlyExceptionFilter()));

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

            services.AddDbContext <PotentialClientsContext>(options =>
                                                            options.UseSqlServer(Configuration.GetConnectionString("PotentialClientsContext")));

            DomainModule.ConfigureServices(services);
            EFCoreModule.ConfigureServices(services);
        }