예제 #1
0
        public ActionResult Index()
        {
            var testSection = NConfigurator.Default.GetSection <TestConfigSection>();

            var configManagerTestSection = ConfigurationManager.GetSection("TestConfigSection") as TestConfigSection;

            var namedTestSection = NConfigurator.UsingFile(@"Config\Custom.config").GetSection <TestConfigSection>("NamedSection");

            ViewBag.NConfigDefault       = testSection.TestValue;
            ViewBag.ConfigurationManager = configManagerTestSection.TestValue;
            ViewBag.NConfigNamed         = namedTestSection.TestValue;

            ViewBag.AppSettings = new List <string>();
            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                ViewBag.AppSettings.Add(key + " : " + ConfigurationManager.AppSettings[key]);
            }

            ViewBag.ConnectionStrings = new List <string>();
            foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
            {
                ViewBag.ConnectionStrings.Add(connectionString.Name + " : " + connectionString.ConnectionString);
            }

            return(View());
        }
예제 #2
0
        public void Should_merge_default_ConectStrings_with_defined_in_file()
        {
            var connStrings = NConfigurator.UsingFile("Configs\\NConfigTest.config").ConnectionStrings;

            Assert.That(connStrings.Count, Is.EqualTo(4));
            Assert.That(connStrings["TestConnectString"].ConnectionString, Is.EqualTo("NConfigTest"));
        }
예제 #3
0
        public void Should_pass_sections_to_section_merger_starting_from_most_important()
        {
            NSectionMerger <TestSection> mergerStub = MockRepository.GenerateStub <NSectionMerger <TestSection> >();

            // Just concatenate all section Values.
            Func <IEnumerable <TestSection>, TestSection> mergeFunc = sections => new TestSection()
            {
                Value = sections.Aggregate("", (v, s) => v += s.Value)
            };

            mergerStub.Stub(m => m.Merge((IEnumerable <TestSection>)null)).IgnoreArguments().Do(mergeFunc);

            try
            {
                NConfigurator.RegisterSectionMerger(mergerStub);
                var testSection = NConfigurator.UsingFiles("Configs\\Aliased.config", "Configs\\NConfigTest.config").GetSection <TestSection>();

                //Concatenated values should be concatenated in order: from the most important section to lower.
                Assert.That(testSection.Value, Is.EqualTo("Tests.AliasedAliased"));
            }
            finally
            {
                NConfigurator.RegisterSectionMerger <TestSection>(new DefaultMerger());
            }
        }
예제 #4
0
        static void Main()
        {
            Console.WriteLine("Simple NConfig Sample. \r\n");

            // Setup NConfigurator to use Custom.config file from Config subfolder.
            NConfigurator.UsingFile(@"Config\Custom.config").SetAsSystemDefault();


            var testSection = NConfigurator.Default.GetSection <TestConfigSection>();

            var configManagerTestSection = ConfigurationManager.GetSection("TestConfigSection") as TestConfigSection;

            var namedTestSection = NConfigurator.UsingFile(@"Config\Custom.config").GetSection <TestConfigSection>("NamedSection");


            Console.WriteLine("NConfig Default : {0} \r\n".F(testSection.TestValue));
            Console.WriteLine("ConfigurationManager : {0} \r\n".F(configManagerTestSection.TestValue));
            Console.WriteLine("NConfig named section : {0} \r\n".F(namedTestSection.TestValue));

            Console.WriteLine("");
            Console.WriteLine("MERGED APP SETTINGS : ");
            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                Console.WriteLine(key + " : " + ConfigurationManager.AppSettings[key]);
            }

            Console.WriteLine("");
            Console.WriteLine("MERGED CONNECTION STRINGS : ");
            foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
            {
                Console.WriteLine(connectionString.Name + " : " + connectionString.ConnectionString);
            }

            Console.ReadKey();
        }
예제 #5
0
        protected void Page_Load(object sender, EventArgs e)
        {
            var testSection = NConfigurator.Default.GetSection <TestConfigSection>();

            var configManagerTestSection = ConfigurationManager.GetSection("TestConfigSection") as TestConfigSection;

            var namedTestSection = NConfigurator.UsingFile(@"Config\Custom.config").GetSection <TestConfigSection>("NamedSection");

            NConfigDefault.InnerText = testSection.TestValue;
            ConfigManager.InnerText  = configManagerTestSection.TestValue;
            NConfigNamed.InnerText   = namedTestSection.TestValue;


            var appSettings = "";

            foreach (var key in ConfigurationManager.AppSettings.AllKeys)
            {
                appSettings += "<p>" + key + " : " + ConfigurationManager.AppSettings[key] + "</p>";
            }
            AppSettings.InnerHtml = appSettings;

            var connectionStrings = "";

            foreach (ConnectionStringSettings connectionString in ConfigurationManager.ConnectionStrings)
            {
                connectionStrings += "<p>" + connectionString.Name + " : " + connectionString.ConnectionString + "</p>";
            }
            ConnectionStrings.InnerHtml = connectionStrings;
        }
예제 #6
0
        public void Should_merge_default_AppSettings_with_defined_in_file()
        {
            var settings = NConfigurator.UsingFile("Configs\\NConfigTest.config").AppSettings;

            Assert.That(settings.Count, Is.EqualTo(3));
            Assert.That(settings["Test"], Is.EqualTo("NConfigTest"));
        }
예제 #7
0
 protected void Application_Start()
 {
     AreaRegistration.RegisterAllAreas();
     FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
     RouteConfig.RegisterRoutes(RouteTable.Routes);
     BundleConfig.RegisterBundles(BundleTable.Bundles);
     NConfigurator.UsingFile(@"Secrets.config").SetAsSystemDefault();
 }
예제 #8
0
        static MvcApplication()
        {
            // This will simulate single core CPU so we can check how configuration substitution works on such systems.
            // System.Diagnostics.Process.GetCurrentProcess().ProcessorAffinity = (System.IntPtr)1;

            // We need to initialize NConfigurator once per AppDomain. Static ctor works well for this purpose.
            NConfigurator.UsingFile(@"~\Config\Custom.config").SetAsSystemDefault();
        }
예제 #9
0
        static void Main(string[] args)
        {
            NConfigurator.UsingFile("ClientSvc.config").SetAsSystemDefault();

            Thread.Sleep(1000); // this will get WCF service app some fora to run.
            Run("WsHttpSimple");

            Console.ReadLine();
        }
예제 #10
0
        public ProblemRptControllerTests()
        {
            var nconfiguration = NConfigurator.UsingFiles(@".\Config\Operations.config");

            if (!_setNconfigAsSystemDefault)
            {
                nconfiguration.SetAsSystemDefault();
                _setNconfigAsSystemDefault = true;
            }
        }
예제 #11
0
        public void Should_return_definded_values_when_only_filled_section_provided()
        {
            var mergerUnderTest = new PropertyMerger <MergeSection>();
            var fullSection     = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("FullSection");

            var resultSection = mergerUnderTest.Merge(new[] { fullSection });

            Assert.That(resultSection.IntValue, Is.EqualTo(4321));
            Assert.That(resultSection.BoolValue, Is.EqualTo(false));
            Assert.That(resultSection.StringValue, Is.EqualTo("FULL"));
        }
예제 #12
0
        public void Should_return_default_values_when_only_empty_section_provided()
        {
            var mergerUnderTest = new PropertyMerger <MergeSection>();
            var emptySection    = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("EmptySection");

            var resultSection = mergerUnderTest.Merge(new[] { emptySection });

            Assert.That(resultSection.IntValue, Is.EqualTo(1234));
            Assert.That(resultSection.BoolValue, Is.EqualTo(true));
            Assert.That(resultSection.StringValue, Is.EqualTo("DEFAULT"));
        }
예제 #13
0
        static void Main(string[] args)
        {
            NConfigurator.UsingFile("HostSvc.config").SetAsSystemDefault();

            ServiceHost host = new ServiceHost(typeof(SimpleService));

            host.Open();

            Console.WriteLine("Host Started at " + host.ChannelDispatchers[0].Listener.Uri);
            Console.ReadLine();
            host.Close();
        }
예제 #14
0
        public void Should_not_override_with_default_values_when_several_sections_provided()
        {
            var mergerUnderTest = new PropertyMerger <MergeSection>();
            var fullSection     = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("FullSection");
            var emptySection    = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("EmptySection");

            var resultSection = mergerUnderTest.Merge(new[] { emptySection, fullSection });

            Assert.That(resultSection.IntValue, Is.EqualTo(4321));
            Assert.That(resultSection.BoolValue, Is.EqualTo(false));
            Assert.That(resultSection.StringValue, Is.EqualTo("FULL"));
        }
예제 #15
0
        public void Should_return_overrided_values_when_several_sections_provided()
        {
            var mergerUnderTest = new PropertyMerger <MergeSection>();
            var fullSection     = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("FullSection");
            var overrideSection = NConfigurator.UsingFile("Configs\\MergeTest.config").GetSection <MergeSection>("OverrideSection");

            var resultSection = mergerUnderTest.Merge(new[] { overrideSection, fullSection });

            Assert.That(resultSection.IntValue, Is.EqualTo(4321));
            Assert.That(resultSection.BoolValue, Is.EqualTo(true));
            Assert.That(resultSection.StringValue, Is.EqualTo("OVERRIDE"));
        }
예제 #16
0
        public void Should_promote_custom_configuration_to_default()
        {
            try
            {
                NConfigurator.UsingFile("Configs\\NConfigTest.config").SetAsDefault();

                Assert.That(NConfigurator.Default.FileNames, Is.EqualTo(NConfigurator.UsingFile("Configs\\NConfigTest.config").FileNames));
                Assert.That(NConfigurator.Default.ConnectionStrings, Is.EqualTo(NConfigurator.UsingFile("Configs\\NConfigTest.config").ConnectionStrings));
                Assert.That(NConfigurator.Default.AppSettings, Is.EqualTo(NConfigurator.UsingFile("Configs\\NConfigTest.config").AppSettings));
            }
            finally
            {
                NConfigurator.UsingFiles().SetAsDefault(); // Restore default Default
            }
        }
예제 #17
0
        public void Should_dump_correct_diagnostics()
        {
            var diagnostics =
                NConfigurator.UsingFiles("NotExisting.config", "Configs\\Aliased.config").DumpDiagnostics();

            Assert.That(diagnostics, Is.StringContaining("NConfig Diagnostics"));
            Assert.That(diagnostics, Is.StringContaining("Host name: " + Environment.MachineName));
            Assert.That(diagnostics, Is.StringContaining("Host alias: Tests"));
            Assert.That(diagnostics, Is.StringContaining("Web Environment: False"));

            Assert.That(diagnostics, Is.StringContaining("missing file: 'Tests.NotExisting.config' location:"));
            Assert.That(diagnostics, Is.StringContaining("missing file: 'NotExisting.config' location:"));
            Assert.That(diagnostics, Is.StringContaining(@"exists file: 'Configs\Tests.Aliased.config' location:"));
            Assert.That(diagnostics, Is.StringContaining(@"exists file: 'Configs\Aliased.config' location:"));
        }
예제 #18
0
        private DeepMergerTestSection GetMergedConfig()
        {
            var merger = new DeepMerger <DeepMergerTestSection>();

            NConfigurator.RegisterSectionMerger(merger);

            IEnumerable <FileInfo> files = GetFiles();

            string[] configFileNames = files
                                       .Select(x => x.FullName)
                                       .ToArray();
            return(NConfigurator
                   .UsingFiles(configFileNames)
                   .GetSection <DeepMergerTestSection>("mergeTest"));
        }
예제 #19
0
        static void Main(string[] args)
        {
            NConfigurator.UsingFile(@"Secrets.config").SetAsSystemDefault();

            using (DocumentClient client = new DocumentClient(
                       new Uri(ConfigurationManager.AppSettings["CosmosDBUri"]),
                       ConfigurationManager.AppSettings["CosmosDBKey"],
                       new ConnectionPolicy {
                ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp
            }))
            {
                var graph = client.ReadDocumentCollectionAsync(ConfigurationManager.AppSettings["CosmosDBCollectionLink"]).Result;

                LoadAirports(client, graph).Wait();
            }
        }
예제 #20
0
        public void Should_promote_custom_configuration_to_system_default()
        {
            try
            {
                var connectStrings = NConfigurator.UsingFile("Configs\\NConfigTest.config").ConnectionStrings;
                var appSettings    = NConfigurator.UsingFile("Configs\\NConfigTest.config").AppSettings;
                NConfigurator.UsingFile("Configs\\NConfigTest.config").SetAsSystemDefault();

                Assert.That(ConfigurationManager.ConnectionStrings, Is.EqualTo(connectStrings));
                Assert.That(ConfigurationManager.AppSettings, Is.EqualTo(appSettings));
            }
            finally
            {
                NConfigurator.RestoreSystemDefaults();
            }
        }
예제 #21
0
        public void Should_call_section_merger_registered_for_section_type()
        {
            NSectionMerger <TestSection> mergerStub = MockRepository.GenerateStub <NSectionMerger <TestSection> >();

            mergerStub.Stub(m => m.Merge((IEnumerable <TestSection>)null)).IgnoreArguments().Return(null);

            try
            {
                NConfigurator.RegisterSectionMerger <TestSection>(mergerStub);
                var testSection = NConfigurator.UsingFiles("Configs\\Aliased.config", "Configs\\NConfigTest.config").GetSection <TestSection>();

                mergerStub.AssertWasCalled(m => m.Merge((IEnumerable <TestSection>)null), opt => opt.IgnoreArguments());
            }
            finally
            {
                NConfigurator.RegisterSectionMerger <TestSection>(new DefaultMerger());
            }
        }
예제 #22
0
        public void Should_dump_correct_diagnostics()
        {
            var configuration = NConfigurator.UsingFiles("NotExisting.config", "Configs\\Aliased.config");

            // populate config cache to get consistent diagnostic results
            configuration.GetSection <TestSection>();

            var diagnostics = configuration.DumpDiagnostics();

            Assert.That(diagnostics, Is.StringContaining("NConfig Diagnostics"));
            Assert.That(diagnostics, Is.StringContaining("Host name: " + Environment.MachineName));
            Assert.That(diagnostics, Is.StringContaining("Host alias: Tests"));
            Assert.That(diagnostics, Is.StringContaining("Web Environment: False"));

            Assert.That(diagnostics, Is.StringContaining("missing file: 'Tests.NotExisting.config' location:"));
            Assert.That(diagnostics, Is.StringContaining("missing file: 'NotExisting.config' location:"));
            Assert.That(diagnostics, Is.StringContaining(@"applied file: 'Configs\Tests.Aliased.config' location:"));
            Assert.That(diagnostics, Is.StringContaining(@"applied file: 'Configs\Aliased.config' location:"));
        }
예제 #23
0
        public void ConnectionStrings_MustRead_AzureConfiguredEnvironmentVars(string azureConnectionStringEnvironmentVarPrefix, string providerName)
        {
            const string connectionStringKey   = "some-azure-portal-connectionstring";
            const string connectionStringValue = "some_value";

            try
            {
                Environment.SetEnvironmentVariable(azureConnectionStringEnvironmentVarPrefix + connectionStringKey, connectionStringValue, EnvironmentVariableTarget.Process);

                var connectionStringsSection = NConfigurator.UsingFile("Configs\\NConfigTest.config").GetSection <ConnectionStringsSection>("ConnectionStrings");

                Assert.That(connectionStringsSection.ConnectionStrings[connectionStringKey], Is.Not.Null, string.Format("the environment var '{0}{1}' should be added to the connectionStrings as '{1}'", azureConnectionStringEnvironmentVarPrefix, connectionStringKey));
                Assert.That(connectionStringsSection.ConnectionStrings[connectionStringKey].ConnectionString, Is.StringMatching(connectionStringValue));
                Assert.That(connectionStringsSection.ConnectionStrings[connectionStringKey].ProviderName, Is.StringMatching(providerName));
            }
            finally
            {
                Environment.SetEnvironmentVariable(azureConnectionStringEnvironmentVarPrefix + connectionStringKey, null, EnvironmentVariableTarget.Process);
            }
        }
예제 #24
0
        public void AppSettings_MustRead_AzureConfiguredEnvironmentVars()
        {
            const string appSettingKey   = "some-azure-portal-appsetting";
            const string appSettingValue = "some_value";
            const string azureAppSettingEnvironmentVarPrefix = "APPSETTING_";

            try
            {
                Environment.SetEnvironmentVariable(azureAppSettingEnvironmentVarPrefix + appSettingKey, appSettingValue, EnvironmentVariableTarget.Process);

                var appSettingsSection = NConfigurator.UsingFile("Configs\\NConfigTest.config").GetSection <AppSettingsSection>("AppSettings");

                Assert.That(appSettingsSection.Settings.AllKeys, Contains.Item(appSettingKey));
                Assert.That(appSettingsSection.Settings[appSettingKey].Value, Is.StringMatching(appSettingValue));
            }
            finally
            {
                Environment.SetEnvironmentVariable(azureAppSettingEnvironmentVarPrefix + appSettingKey, null, EnvironmentVariableTarget.Process);
            }
        }
예제 #25
0
        public void Should_merge_default_AppSettings_with_defined_in_file_and_azure_environmentvar_should_take_precedence()
        {
            const string appSettingKey   = "test";
            const string appSettingValue = "some_value_from_azure_env_vars";
            const string azureAppSettingEnvironmentVarPrefix = "APPSETTING_";

            try
            {
                Environment.SetEnvironmentVariable(azureAppSettingEnvironmentVarPrefix + appSettingKey, appSettingValue, EnvironmentVariableTarget.Process);

                var appSettingsSection = NConfigurator.UsingFile("Configs\\NConfigTest.config").GetSection <AppSettingsSection>("AppSettings");

                Assert.That(appSettingsSection.Settings.AllKeys, Contains.Item(appSettingKey));
                Assert.That(appSettingsSection.Settings[appSettingKey].Value, Is.StringMatching(appSettingValue));
            }
            finally
            {
                Environment.SetEnvironmentVariable(azureAppSettingEnvironmentVarPrefix + appSettingKey, null, EnvironmentVariableTarget.Process);
            }
        }
        protected void Application_Start()
        {
            NConfigurator.UsingFiles(@"~\Config\Operations.config").SetAsSystemDefault();
            LogManager.ReconfigExistingLoggers();

            AreaRegistration.RegisterAllAreas();
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);

            AppDomain.CurrentDomain.UnhandledException += (sender, e) =>
            {
                try
                {
                    var logger    = LogManager.GetCurrentClassLogger();
                    var exception = e.ExceptionObject as Exception;
                    logger.Error(exception, "Unhandled exception in AppDomain occured, rethrowing...");
                }
                catch
                {
                }
            };
        }
예제 #27
0
        static void Main()
        {
            Console.WriteLine("DeepMerger NConfig Sample. \r\n");

            // Registering the correct merger for my section
            NConfigurator.RegisterSectionMerger(new DeepMerger <TestConfigSection>());

            // Setup NConfigurator to use Custom.config file from Config subfolder.
            NConfigurator.UsingFile(@"Config\Custom.config").SetAsSystemDefault();

            var testSection = NConfigurator.Default.GetSection <TestConfigSection>();


            Console.WriteLine("Property Attribute: {0} \r\n".F(testSection.TestValue.Attribute));

            Console.WriteLine("");
            Console.WriteLine("MERGED COLLECTION: ");
            foreach (var key in testSection.TestValue.Collection)
            {
                Console.WriteLine(key.Key + " : " + key.Attribute);
            }

            Console.ReadKey();
        }
예제 #28
0
        public void Should_override_ConnectString_by_the_most_recent_config_file()
        {
            var conString = NConfigurator.UsingFiles("Configs\\Aliased.config", "Configs\\NConfigTest.config").ConnectionStrings["TestConnectString"].ConnectionString;

            Assert.That(conString, Is.EqualTo("Aliased"));
        }
예제 #29
0
        public void Should_override_sections_by_the_most_recent_config_file()
        {
            var testSection = NConfigurator.UsingFiles("Configs\\Aliased.config", "Configs\\NConfigTest.config").GetSection <TestSection>();

            Assert.That(testSection.Value, Is.EqualTo("Tests.Aliased"));
        }
예제 #30
0
        public void Should_provide_default_AppSettings_for_non_existing_file()
        {
            var settings = NConfigurator.UsingFile("NotExisting.config").AppSettings;

            Assert.That(settings, Is.EqualTo(ConfigurationManager.AppSettings));
        }