Ejemplo n.º 1
0
        public new void SetUp()
        {
            var integrationConfig = new LocationRequesterConfiguration("apiKey", 10);

            httpClient = new Mock <IHttpClient>();
            system     = new LocationRequester(integrationConfig, new Mock <Serilog.ILogger>().Object, httpClient.Object, context);

            context.Locations.RemoveRange(context.Locations);
            context.SaveChanges();
        }
Ejemplo 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.AddLogging(loggingBuilder => loggingBuilder.AddSerilog(dispose: true));

            var connectionString = new EnvConfigConnectionStringBuilder().GetConnectionString(Configuration);

            services.AddEntityFrameworkNpgsql()
            .AddDbContext <CourseDbContext>(
                options =>
            {
                const int maxRetryCount        = 3;
                const int maxRetryDelaySeconds = 5;

                var postgresErrorCodesToConsiderTransient = new List <string>();        // ref: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/blob/16c8d07368cb92e10010b646098b562ecd5815d6/src/EFCore.PG/NpgsqlRetryingExecutionStrategy.cs#L99

                // Note that the retry will only retry for TimeoutExceptions and transient postgres exceptions. ref: https://github.com/npgsql/Npgsql.EntityFrameworkCore.PostgreSQL/blob/8e97e4195b197ae3d16763704352acfffa95c73f/src/EFCore.PG/Storage/Internal/NpgsqlTransientExceptionDetector.cs#L12
                options.UseNpgsql(connectionString,
                                  b => b.MigrationsAssembly((typeof(CourseDbContext).Assembly).ToString())
                                  .EnableRetryOnFailure(maxRetryCount, TimeSpan.FromSeconds(maxRetryDelaySeconds), postgresErrorCodesToConsiderTransient));
            });

            services.AddMvc().AddJsonOptions(
                options =>
            {
                options.SerializerSettings.ContractResolver           = new CamelCasePropertyNamesContractResolver();
                options.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects;
            }
                );
            services.AddScoped <ICourseDbContext>(provider => provider.GetService <CourseDbContext>());
            services.AddScoped(provider => new HttpClient());
            services.AddSingleton <IHttpClient>(provider => new WrappedHttpClient());
            services.AddSingleton <LocationRequesterConfiguration>(provider => LocationRequesterConfiguration.FromConfiguration(Configuration));
            services.AddSingleton <Serilog.ILogger>(provider => new LoggerConfiguration()
                                                    .ReadFrom.Configuration(Configuration)
                                                    .WriteTo
                                                    .ApplicationInsightsTraces(Configuration["APPINSIGHTS_INSTRUMENTATIONKEY"])
                                                    .Enrich.WithProperty("Type", "SearchAndCompareGeocoder")
                                                    .Enrich.WithProperty("Identifer", Guid.NewGuid())
                                                    .CreateLogger());

            services.AddScoped <ILocationRequester, LocationRequester>();

            // No default auth method has been set here because each action must explicitly be decorated with
            // ApiTokenAuthAttribute.
            services.AddAuthentication()
            .AddBearerTokenApiKey(options =>
            {
                options.ApiKey = Configuration["api:key"];
            });
        }