public void NonObjectRootIsInvalid()
        {
            var json = @"'test'";
            var jsonConfigSource = new JsonConfigurationProvider(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 JsonConfigurationProvider("path", true);
     using (var stream = typeof(EmbeddedConfiguration).GetTypeInfo().Assembly.GetManifestResourceStream("Autofac.Configuration.Test.Files." + configFile))
     {
         typeof(JsonConfigurationProvider).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 JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);

            jsonConfigSrc.Load(TestStreamHelpers.StringToStream(json));

            Assert.Equal(string.Empty, jsonConfigSrc.Get("name"));
        }
예제 #4
0
        public Startup(IApplicationEnvironment app, ILoggerFactory factory)
        {
            var jsonConfig = new JsonConfigurationProvider("config.json");
            var environConfig = new EnvironmentVariablesConfigurationProvider();

            var builder = new ConfigurationBuilder();
            builder.Add(jsonConfig);
            builder.Add(environConfig);
            builder.SetBasePath(app.ApplicationBasePath);

            Configuration = builder.Build();
            Environment = app;

            Factory = factory;
        }
        public void SupportAndIgnoreComments()
        {
            var json = @"/* Comments */
                {/* Comments */
                ""name"": /* Comments */ ""test"",
                ""address"": {
                    ""street"": ""Something street"", /* Comments */
                    ""zipcode"": ""12345""
                }
            }";
            var jsonConfigSrc = new JsonConfigurationProvider(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"));
        }
예제 #6
0
        public void Start()
        {
            var configSource = new JsonConfigurationProvider($@"{_applicationEnvironment.ApplicationBasePath}\config.json");
            
            var config = new ConfigurationBuilder()
                .Add(configSource)
                .Build();

            var builder = new WebHostBuilder(config);
            builder.UseServer("Microsoft.AspNet.Server.Kestrel");
            builder.UseServices(services => services.AddMvc());
            builder.UseStartup(appBuilder =>
            {
                appBuilder.UseDefaultFiles();
                appBuilder.UseStaticFiles();
                appBuilder.UseMvc();
            });

            _application = builder.Build().Start();
        }
        public void LoadKeyValuePairsFromValidJson()
        {
            var json = @"
{
    'firstname': 'test',
    'test.last.name': 'last.name',
        'residential.address': {
            'street.name': 'Something street',
            'zipcode': '12345'
        }
}";
            var jsonConfigSrc = new JsonConfigurationProvider(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"));
        }
예제 #8
0
        protected override void OnStart(string[] args)
        {
            var configSource = new JsonConfigurationProvider($@"{_applicationEnvironment.ApplicationBasePath}\config.json");

            var config = new ConfigurationBuilder().Add(configSource).Build();
            var builder = new WebHostBuilder(config);
            builder.UseServer("Microsoft.AspNet.Server.Kestrel");
            builder.UseServices(services => services.AddMvc());
            builder.UseStartup(appBuilder =>
            {
                appBuilder.UseStaticFiles();

                appBuilder.UseMvc(routes =>
                {
                    routes.MapRoute(
                        "Default",
                        "{controller}/{action}",
                        new { controller = "home", action = "index" });
                });
            });

            _hostingEngine = builder.Build();
            _shutdownServerDisposable = _hostingEngine.Start();
        }
예제 #9
0
 public static void Initialise(IApplicationEnvironment applicationEnvironment)
 {
     var configProvider = new JsonConfigurationProvider($@"{applicationEnvironment.ApplicationBasePath}\config.json");
     Configuration = new ConfigurationBuilder().Add(configProvider).Build();
 }
 public void ThrowExceptionWhenUnexpectedEndFoundBeforeFinishParsing()
 {
     var json = @"{
         'name': 'test',
         'address': {
             'street': 'Something street',
             'zipcode': '12345'
         }
     /* Missing a right brace here*/";
     var jsonConfigSource = new JsonConfigurationProvider(TestStreamHelpers.ArbitraryFilePath);
    
     var exception = Assert.Throws<FormatException>(
         () => jsonConfigSource.Load(TestStreamHelpers.StringToStream(json)));
     Assert.NotNull(exception.Message);
 }
 public void JsonConfiguration_Does_Not_Throw_On_Optional_Configuration()
 {
     var configSource = new JsonConfigurationProvider("NotExistingConfig.json", optional: true);
     configSource.Load();
     Assert.Throws<InvalidOperationException>(() => configSource.Get("key"));
 }
        public void JsonConfiguration_Throws_On_Missing_Configuration_File()
        {
            var configSource = new JsonConfigurationProvider("NotExistingConfig.json", optional: false);
            var exception = Assert.Throws<FileNotFoundException>(() => configSource.Load());

            // Assert
            Assert.Equal(Resources.FormatError_FileNotFound("NotExistingConfig.json"), exception.Message);
        }