private static void Main() { var path = GetApplicationRoot(); Console.WriteLine($"BasePath: {path}"); var config = new ConfigurationBuilder() .SetBasePath(path) .AddJsonFile("appsettings.json", false, true).Build(); var manager = new ConsoleReportingConfigurationManager(config); var services = new ServiceCollection(); services.Configure <ApplicationSettings>(manager); manager.ConfigureValidator(new ApplicationSettingsValidator()); var sp = services.BuildServiceProvider(); sp.UseSettingsValidator(manager); sp.GetService <IOptionsMonitor <ApplicationSettings> >().OnChange(settings => Console.WriteLine($"-> Monitor detected FileChange - new name: {settings.Name}")); Console.WriteLine("Type <Enter> to exit."); Console.WriteLine("Type a non empty string to update appsettings.json"); while (Console.ReadLine() != string.Empty) { File.WriteAllText(Path.Combine(path, "appsettings.json"), $"{{\"AppSettings\": {{\"Name\": \"{RandomString()}\"}}}}"); var singleton = sp.GetService <IOptions <ApplicationSettings> >(); Console.WriteLine($"Singleton using IOptions: {singleton.Value.Name}"); var scoped = sp.CreateScope().ServiceProvider.GetService <IOptionsSnapshot <ApplicationSettings> >(); Console.WriteLine($"Scoped using IOptionsSnapshot: {scoped.Value.Name}"); } }
public void RespectsRazorLightEmbeddedConfig() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailServiceOptions:SmtpServer", "smtp.test.com"), new KeyValuePair <string, string>("MailServiceOptions:FromMail", "*****@*****.**"), new KeyValuePair <string, string>("MailServiceOptions:FromName", "Mail"), new KeyValuePair <string, string>("MailServerCertificateValidationOptions:Validate", "false"), new KeyValuePair <string, string>("MailTemplateOptions:TemplateSource", "Embedded"), new KeyValuePair <string, string>("MailTemplateOptions:DefaultNamespace", "MailViewsEmbedded"), new KeyValuePair <string, string>("MailTemplateOptions:BaseDirectory", "MailViews"), new KeyValuePair <string, string>("MailTemplateOptions:Extension", ".cshtml") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); var environment = new TestHostingEnvironment { ContentRootPath = ApplicationHelper.GetApplicationCodebase() }; services.AddLogging(); services.ConfigureMailServiceTemplated(environment, manager, typeof(GlobalTestSettings)); var s = services.BuildServiceProvider().GetRequiredService <RazorLightProject>(); Assert.IsTrue(s is EmbeddedRazorProject); Assert.IsTrue(s.GetItemAsync("Test").Result.Exists); }
public void AddsRazorLight() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailServiceOptions:SmtpServer", "smtp.test.com"), new KeyValuePair <string, string>("MailServiceOptions:FromMail", "*****@*****.**"), new KeyValuePair <string, string>("MailServiceOptions:FromName", "Mail"), new KeyValuePair <string, string>("MailServerCertificateValidationOptions:Validate", "false"), new KeyValuePair <string, string>("MailTemplateOptions:BaseDirectory", "MailViews"), new KeyValuePair <string, string>("MailTemplateOptions:Extension", ".cshtml") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); var environment = new TestHostingEnvironment { ContentRootPath = ApplicationHelper.GetApplicationCodebase() }; services.AddLogging(); services.ConfigureMailServiceTemplated(environment, manager); var s = services.BuildServiceProvider().GetRequiredService <ITemplatingMailService>(); Assert.IsNotNull(s); }
public void ValidatesMailTemplateOptions() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailTemplateOptions:Extension", "cshtml") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); // raises exception for not configured settings services.ConfigureMailTemplateService(manager); }
public void AddsMailTemplateValidators() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailTemplateOptions:BaseDirectory", "MailViews"), new KeyValuePair <string, string>("MailTemplateOptions:Extension", ".cshtml") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); services.ConfigureMailTemplateService(manager); var validator = manager.Validators[typeof(MailTemplateOptions)]; Assert.IsNotNull(validator); }
public void AddsMailServiceValidators() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailServiceOptions:SmtpServer", "smtp.test.com"), new KeyValuePair <string, string>("MailServiceOptions:FromMail", "*****@*****.**"), new KeyValuePair <string, string>("MailServiceOptions:FromName", "Mail"), new KeyValuePair <string, string>("MailServerCertificateValidationOptions:Validate", "false") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); services.ConfigureMailService(manager); var validator = manager.Validators[typeof(MailServiceOptions)]; Assert.IsNotNull(validator); }
public void AddsMailTemplateOptions() { var services = new ServiceCollection(); var builder = new ConfigurationBuilder() .AddInMemoryCollection(new[] { new KeyValuePair <string, string>("MailTemplateOptions:BaseDirectory", "MailViews"), new KeyValuePair <string, string>("MailTemplateOptions:Extension", ".cshtml") }); var config = builder.Build(); var manager = new ConsoleReportingConfigurationManager(config); services.ConfigureMailTemplateService(manager); var provider = services.BuildServiceProvider(); var settings = provider.GetService <MailTemplateOptions>(); Assert.IsNotNull(settings); }
private static IServiceProvider ConfigureServices(IConfigurationRoot config) { try { var environment = new TestHostingEnvironment { ContentRootPath = ApplicationHelper.GetApplicationCodebase() }; var manager = new ConsoleReportingConfigurationManager(config); var services = new ServiceCollection(); services.ConfigureMailServiceTemplated(environment, manager); services.Configure <ApplicationSettings>(manager, true); services.AddLogging(builder => builder .AddFilter("Microsoft", LogLevel.Warning) .AddFilter("System", LogLevel.Warning) .AddConsole()); return(services.BuildServiceProvider()); } catch (MissingSettingException e) { Console.WriteLine(e); throw; } }