/// <summary>Uses the settings validator.</summary> /// <param name="serviceProvider">The service provider.</param> /// <param name="manager">The manager.</param> /// <exception cref="ArgumentNullException">serviceProvider or manager</exception> public static IServiceProvider UseSettingsValidator(this IServiceProvider serviceProvider, ValidatingConfigurationManager manager) { if (serviceProvider == null) { throw new ArgumentNullException(nameof(serviceProvider)); } if (manager == null) { throw new ArgumentNullException(nameof(manager)); } foreach (var monitor in manager.Validators.Keys .Select(settingsType => typeof(IOptionsMonitor <>) .MakeGenericType(settingsType)) .Select(monitorType => serviceProvider.GetService(monitorType) as IOptionsMonitor <object>)) { if (monitor != null) { manager.KeepValidating(monitor); } } return(serviceProvider); }
/// <summary>Configures the mail template service.</summary> /// <param name="services">The services.</param> /// <param name="manager">The manager.</param> /// <returns></returns> public static IServiceCollection ConfigureMailTemplateService(this IServiceCollection services, ValidatingConfigurationManager manager) { // configure options and validators manager.ConfigureValidator(new MailTemplateOptionsValidator()); services.Configure <MailTemplateOptions>(manager, true); services.AddScoped <ITemplatingService, RazorLightTemplatingService>(); return(services); }
/// <summary>Configures the validator.</summary> /// <typeparam name="TSettings">The type of the settings.</typeparam> /// <param name="manager">The manager.</param> /// <param name="validator">The validator.</param> public static ValidatingConfigurationManager ConfigureValidator <TSettings>( this ValidatingConfigurationManager manager, IValidator <TSettings> validator) { if (manager == null) { throw new ArgumentNullException(nameof(manager)); } if (validator == null) { throw new ArgumentNullException(nameof(validator)); } manager.Validators.Add(typeof(TSettings), validator); return(manager); }
/// <summary>Configures the mail service.</summary> /// <param name="services">The services.</param> /// <param name="manager">The manager.</param> /// <returns></returns> public static IServiceCollection ConfigureMailService(this IServiceCollection services, ValidatingConfigurationManager manager) { // configure options and validators manager.ConfigureValidator(new MailServiceOptionsValidator()); services.Configure <MailServiceOptions>(manager, true); services.Configure <MailServerCertificateValidationOptions>(manager, true); services.AddSingleton <IMailTransportFactory, MailKitSmtpTransportFactory>(); services.AddScoped <IMailService, CertificateValidatingMailService>(provider => new CertificateValidatingMailService( provider.GetRequiredService <IOptionsMonitor <MailServiceOptions> >(), provider.GetService <ILogger <CertificateValidatingMailService> >(), provider.GetRequiredService <IOptionsMonitor <MailServerCertificateValidationOptions> >(), provider.GetRequiredService <IMailTransportFactory>())); return(services); }
/// <summary>Configures the mail service and templates.</summary> /// <param name="services">The services.</param> /// <param name="environment">The HostingEnvironment.</param> /// <param name="manager">The manager.</param> /// <param name="assemblyRootType">A root type of the assembly.</param> /// <returns></returns> // ReSharper disable once UnusedMember.Global public static IServiceCollection ConfigureMailServiceTemplated(this IServiceCollection services, IWebHostEnvironment environment, ValidatingConfigurationManager manager, Type assemblyRootType = null) { return(services .ConfigureMailService(manager) .ConfigureMailTemplateService(manager) .ConfigureRazorLight(environment, assemblyRootType) .AddScoped <ITemplatingMailService, TemplatingMailService>()); }