Пример #1
0
    /// <summary>
    /// Создаёт экземпляры всех настроек и получает значения, чтобы провести процесс валидации при старте приложения.
    /// </summary>
    public static IServiceCollection AddOptionsAndSecretsValidationOnStartup(this IServiceCollection services)
    {
        ////перместить валидацию в общий процесс прогрева https://andrewlock.net/reducing-latency-by-pre-building-singletons-in-asp-net-core/
        try
        {
            HostOptions        hostOptions        = services.BuildServiceProvider().GetService <IOptions <HostOptions> >().Value;
            AwsOptions         awsOptions         = services.BuildServiceProvider().GetService <IOptions <AwsOptions> >().Value;
            ApplicationOptions applicationOptions = services.BuildServiceProvider().GetService <IOptions <ApplicationOptions> >().Value;
            GeneralOptions     generalOptions     = services.BuildServiceProvider().GetService <IOptions <GeneralOptions> >().Value;
            IdempotencyOptions idempotencyOptions = services.BuildServiceProvider().GetService <IOptions <IdempotencyOptions> >().Value;

            AppSecrets appSecrets = services.BuildServiceProvider().GetService <IOptions <AppSecrets> >().Value;
        }
        catch (OptionsValidationException ex)
        {
            Console.WriteLine($"Error validating {ex.OptionsType.FullName}: {string.Join(", ", ex.Failures)}");
            throw;
        }

        return(services);
    }
Пример #2
0
    /// <summary>
    /// Add and configure Swagger services.
    /// </summary>
    public static IServiceCollection AddCustomSwagger(this IServiceCollection services, IdempotencyOptions idempotencyOptions)
    {
        services
        .AddTransient <IConfigureOptions <SwaggerGenOptions>, ConfigureSwaggerOptions>()
        .AddSwaggerGen();

        return(services);
    }
Пример #3
0
    /// <summary>
    /// Configures the services to add to the ASP.NET Core Injection of Control (IoC) container. This method gets called by the ASP.NET runtime. See
    /// http://blogs.msdn.com/b/webdev/archive/2014/06/17/dependency-injection-in-asp-net-vnext.aspx
    /// </summary>
    public void ConfigureServices(IServiceCollection services)
    {
        services
        .AddCustomOptions(_config)
        .AddOptionsAndSecretsValidationOnStartup();

        AppSecrets         secrets            = _config.GetSection(nameof(AppSecrets)).Get <AppSecrets>();
        GeneralOptions     generalOptions     = _config.GetSection(nameof(ApplicationOptions.General)).Get <GeneralOptions>();
        IdempotencyOptions idempotencyOptions = _config
                                                .GetSection(nameof(ApplicationOptions.IdempotencyControl)).Get <IdempotencyOptions>();

        AWSOptions awsOptions = _config.GetAWSOptions();

        services.AddDefaultAWSOptions(awsOptions);

        if (!string.IsNullOrEmpty(secrets.AppInsightsInstrumentationKey))
        {
            ApplicationInsightsServiceOptions options = new ApplicationInsightsServiceOptions
            {
                DeveloperMode      = _webHostEnvironment.IsDevelopment(),
                InstrumentationKey = secrets.AppInsightsInstrumentationKey
            };

            services.AddApplicationInsightsTelemetry(options);
        }

        services
        .AddCorrelationIdFluent(generalOptions)
        .AddCustomCaching()
        .AddCustomCors()
        .AddCustomRouting()
        .AddResponseCaching()
        .AddCustomResponseCompression(_config)
        .AddCustomHealthChecks()
        .AddCustomSwagger(idempotencyOptions)
        .AddFluentValidationRulesToSwagger()
        .AddHttpContextAccessor()

        .AddSingleton <IActionContextAccessor, ActionContextAccessor>()

        .AddCustomApiVersioning();

        services.AddIdempotencyContextLogging(options =>
        {
            options.IdempotencyLogAttribute = "IdempotencyKey";
        });

        services.AddHttpContextLogging(options =>
        {
            options.LogRequestBody  = true;
            options.LogResponseBody = true;
            options.MaxBodyLength   = generalOptions.MaxLogFieldLength;
            options.SkipPaths       = new List <PathString> {
                "/metrics"
            };
        });

        services
        .AddControllers()
        .AddCustomJsonOptions(_webHostEnvironment)
        .AddCustomMvcOptions(_config)
        .AddCustomModelValidation();

        services.AddIdempotencyControl(options =>
        {
            options.Enabled           = idempotencyOptions.IdempotencyFilterEnabled ?? false;
            options.HeaderRequired    = true;
            options.IdempotencyHeader = idempotencyOptions.IdempotencyHeader;
        });

        services.AddHttpClient();
        services.AddMediatR(GetType().Assembly);

        services
        .AddProjectActionHandlers()
        .AddProjectMappers()
        .AddProjectRepositories()
        .AddProjectServices();

        services.AddCustomMessageBus(secrets);
    }