Example #1
0
        public static IServiceCollection AddHealthCheck(this IServiceCollection services)
        {
            AutoMapper.Mapper.Initialize(x => Registrar.Configure(x));

            services.AddHttpContextAccessor();
            services.AddCors(options =>
            {
                options.AddPolicy("AllowAll",
                                  builder =>
                {
                    builder
                    .AllowAnyOrigin()
                    .AllowAnyMethod()
                    .AllowAnyHeader()
                    .AllowCredentials();
                });
            });
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2).AddJsonOptions(options =>
            {
                options.SerializerSettings.ContractResolver      = new Newtonsoft.Json.Serialization.DefaultContractResolver();
                options.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
                options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
                options.SerializerSettings.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Local;
                options.SerializerSettings.DateFormatHandling   = Newtonsoft.Json.DateFormatHandling.IsoDateFormat;
            });
            services.AddSingleton(Configuration);
            services.AddHangfire(x => x.UseSqlServerStorage(Configuration.GetConnectionString("DefaultConnection")));
            services.AddHangfireServer();
            services.AddTransient <IMessagingService, MailService>();


            services.AddScoped((serviceProvider) =>
            {
                var config = serviceProvider.GetRequiredService <IConfiguration>();
                return(new SmtpClient()
                {
                    Host = config.GetValue <String>("Email:Smtp:Host"),
                    Port = config.GetValue <int>("Email:Smtp:Port"),
                    Credentials = new NetworkCredential(
                        config.GetValue <String>("Email:Smtp:Username"),
                        config.GetValue <String>("Email:Smtp:Password")
                        )
                });
            });
            return(services);
        }