private static async Task Run(string[] args) { try { // This wont work... // See https://github.com/aspnet/Configuration/issues/214 //var config = new ConfigurationBuilder(); //config.AddJsonFile("wiki.json"); var jsonConfig = new JsonConfigurationSource("wiki.json"); var config = new ConfigurationBuilder(jsonConfig); var builder = new ContainerBuilder(); var module = new ConfigurationModule(config.Build()); builder.RegisterModule(module); var loggerFactory = new LoggerFactory(); loggerFactory.AddConsole(LogLevel.Information); builder.RegisterInstance(loggerFactory).As<ILoggerFactory>().SingleInstance(); builder.RegisterType<GetDescriptionAction>().AsImplementedInterfaces().InstancePerDependency(); builder.RegisterType<AddValueToAlternativeKeysAction>().AsImplementedInterfaces().InstancePerDependency(); builder.Register(ctx => new CompositeDictEntryAction(ctx.Resolve<IEnumerable<IDictEntryAction>>())) .Keyed<IDictEntryAction>(DictEntryActionKey); builder.Register( ctx => new Wiki.Wiki(ctx.Resolve<HttpClient>(), ctx.ResolveOptionalKeyed<IDictEntryAction>(DictEntryActionKey), ctx.Resolve<ILoggerFactory>())) .AsImplementedInterfaces() .InstancePerDependency(); builder.RegisterInstance(new DictConfig { FilePath = Path.Combine("..", "..", "resources", "kindle_dict.html"), OpfFilePath = Path.Combine("..", "..", "resources"), TemplateFilePath = Path.Combine("..", "..", "resources", "knidle_dict_template.html"), EntryTemplateFilePath = Path.Combine("..", "..", "resources", "knidle_dict_entry_template.html"), OpfTemplateFilePath = Path.Combine("..", "..", "resources", "kindle_dict_template.opf"), }).SingleInstance(); builder.RegisterType<Dict>().AsImplementedInterfaces().SingleInstance(); var logger = loggerFactory.CreateLogger(typeof (Program).FullName); var container = builder.Build(); var wiki = container.Resolve<IWiki>(); logger.LogInformation("Getting wiki description..."); var wikiDesc = await wiki.GetDescriptionAsync().ConfigureAwait(false); logger.LogInformation("Getting entries..."); var entries = await wiki.GetEntriesAsync().ConfigureAwait(false); var dict = container.Resolve<IDict>(); logger.LogInformation("Saving dictionary..."); await dict.SaveAsync(wikiDesc, entries).ConfigureAwait(false); } catch (Exception ex) { Console.WriteLine(ex.Message); Console.WriteLine(ex.StackTrace); } }
public void JsonConfiguration_Throws_On_Missing_Configuration_File() { var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: false); var exception = Assert.Throws<FileNotFoundException>(() => configSource.Load()); // Assert Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message); }
public void NonObjectRootIsInvalid() { var json = @"'test'"; var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath); var exception = Assert.Throws<FormatException>( () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json))); Assert.NotNull(exception.Message); }
public static IConfiguration LoadJson(string configFile) { var config = new ConfigurationBuilder(); var source = new JsonConfigurationSource("path", true); using (var stream = typeof(EmbeddedConfiguration).GetTypeInfo().Assembly.GetManifestResourceStream("Autofac.Configuration.Test.Files." + configFile)) { typeof(JsonConfigurationSource).GetMethod("Load", BindingFlags.Instance | BindingFlags.NonPublic).Invoke(source, new object[] { stream }); } config.Add(source, false); return config.Build(); }
public void LoadMethodCanHandleEmptyValue() { var json = @" { 'name': '' }"; var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath); jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal(string.Empty, jsonConfigSrc.Get("name")); }
public void LoadKeyValuePairsFromValidJson() { var json = @" { 'firstname': 'test', 'test.last.name': 'last.name', 'residential.address': { 'street.name': 'Something street', 'zipcode': '12345' } }"; var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath); jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("test", jsonConfigSrc.Get("firstname")); Assert.Equal("last.name", jsonConfigSrc.Get("test.last.name")); Assert.Equal("Something street", jsonConfigSrc.Get("residential.address:STREET.name")); Assert.Equal("12345", jsonConfigSrc.Get("residential.address:zipcode")); }
public void SupportAndIgnoreComments() { var json = @"/* Comments */ {/* Comments */ ""name"": /* Comments */ ""test"", ""address"": { ""street"": ""Something street"", /* Comments */ ""zipcode"": ""12345"" } }"; var jsonConfigSrc = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath); jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json)); Assert.Equal("test", jsonConfigSrc.Get("name")); Assert.Equal("Something street", jsonConfigSrc.Get("address:street")); Assert.Equal("12345", jsonConfigSrc.Get("address:zipcode")); }
public void JsonConfiguration_Does_Not_Throw_On_Optional_Configuration() { var configSource = new JsonConfigurationSource("NotExistingConfig.json", optional: true); configSource.Load(); Assert.Throws<InvalidOperationException>(() => configSource.Get("key")); }
public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing() { var json = @"{ 'name': 'test', 'address': { 'street': 'Something street', 'zipcode': '12345' } /* Missing a right brace here*/"; var jsonConfigSource = new JsonConfigurationSource(TestStreamHelpers.ArbitraryFilePath); var exception = Assert.Throws<FormatException>( () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json))); Assert.NotNull(exception.Message); }