/// <summary>
        ///
        /// </summary>
        /// <param name="services"></param>
        public static void AddHealthChecksSetup(this IServiceCollection services,
                                                IConfiguration configuration)
        {
            var settings = HealthCheckSettings.FromConfiguration(configuration);

            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            string cnString = configuration.GetConnectionString("DefaultConnection");

            services.AddHealthChecksUI(setupSettings: options =>
            {
                var settings = HealthCheckSettings.FromConfiguration(configuration);
                if (settings != null)
                {
                    options.AddHealthCheckEndpoint(
                        settings.Name,
                        settings.Uri
                        );
                    options.SetHealthCheckDatabaseConnectionString(settings.HealthCheckDatabaseConnectionString);
                    options.SetEvaluationTimeInSeconds(settings.EvaluationTimeinSeconds);
                    options.SetMinimumSecondsBetweenFailureNotifications(settings.MinimumSecondsBetweenFailureNotifications);
                }
            });

            services.AddHealthChecks()
            .AddCheck("ContactBookCQRS-check",
                      new SqlConnectionHealthCheck(cnString),
                      HealthStatus.Unhealthy, new string[] { "ContactBookCQRS" });
        }
Beispiel #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            // Settings
            services
            .AddOptions()
            .ConfigureAndValidate <IdentitySettings>(Configuration)    //If settings are not valid, the server does not launch
            .Configure <SmtpSettings>(Configuration.GetSection("Smtp"))
            .Configure <AzureStorageSettings>(Configuration.GetSection("AzureStorage"));

            // Dependancy Injection
            services.AddAutofac();

            // Identity
            services
            .AddIdentity <User, Role>(options =>
            {
                options.SignIn.RequireConfirmedEmail = true;
                options.Lockout.AllowedForNewUsers   = false;
            })
            .AddEntityFrameworkStores <AllInOneDbContext>()
            .AddDefaultTokenProviders()
            .AddUserStore <UserStore <User, Role, AllInOneDbContext, Guid, IdentityUserClaim <Guid>, UserRole, IdentityUserLogin <Guid>, IdentityUserToken <Guid>, IdentityRoleClaim <Guid> > >()
            .AddRoleStore <RoleStore <Role, AllInOneDbContext, Guid, UserRole, IdentityRoleClaim <Guid> > >();

            //Caching response for middlewares
            services.AddResponseCaching();

            // Authentication
            services.RegisterAuthentication();

            // SignalR
            services.AddSignalR();
            services.AddSingleton <IUserIdProvider, UserIdProvider>();

            // Insights
            services.AddApplicationInsightsTelemetry(Configuration);

            // Add framework services.
            services.AddControllers(options =>
            {
                //options.EnableEndpointRouting = false;
                options.Filters.AddService(typeof(ApiExceptionFilter));
                options.Filters.Add(typeof(ValidateModelStateAttribute));
            }).AddNewtonsoftJson(options =>
            {
                options.SerializerSettings.Converters.Add(new StringEnumConverter());
                options.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
            }).SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

            // Swagger-ui
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo
                {
                    Title       = $"{Common.Constants.Project.Name} API",
                    Version     = "v1",
                    Description = $"Welcome to the marvellous {Common.Constants.Project.Name} API!",
                });
                c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "JWT Authorization header using the Bearer scheme. Example: \"Authorization: Bearer {token}\"",
                    Name        = "Authorization",
                    In          = ParameterLocation.Header,
                    Type        = SecuritySchemeType.ApiKey
                });
                c.AddSecurityRequirement(new OpenApiSecurityRequirement()
                {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference {
                                Type = ReferenceType.SecurityScheme, Id = "Bearer"
                            }
                        },
                        new[] { "readAccess", "writeAccess" }
                    }
                });
            });
            services.AddSwaggerDocument();

            // HealthCheck
            services.AddHealthChecksUI(setupSettings: options =>
            {
                var settings = HealthCheckSettings.FromConfiguration(Configuration);
                if (settings != null)
                {
                    options.AddHealthCheckEndpoint(
                        settings.Name,
                        settings.Uri
                        );
                    options.SetHealthCheckDatabaseConnectionString(settings.HealthCheckDatabaseConnectionString);
                    options.SetEvaluationTimeInSeconds(settings.EvaluationTimeinSeconds);
                    options.SetMinimumSecondsBetweenFailureNotifications(settings.MinimumSecondsBetweenFailureNotifications);
                }
            });
            services.AddHealthChecks()
            .AddCheck <SmtpHealthCheck>("SMTP")
            .AddCheck <AzureStorageHealthCheck>("AzureStorage")
            .AddCheckSettings <IdentitySettings>()    //new way to validate settings
            .AddCheckSettings <AzureStorageSettings>()
            .AddDbContextCheck <AllInOneDbContext>("Default");

            // Profiling
            services.AddMemoryCache();
            services.AddMiniProfiler(options =>
                                     options.RouteBasePath = "/profiler"
                                     );

            // Automapper
            services.AddAutoMapper(typeof(Startup).Assembly);

            // Controllers
            services.AddControllers();

            // OverWritable services (for testing)
            SetUpDataBase(services);
        }