当前位置: 首页>>代码示例>>C#>>正文


C# Configuration.TryGet方法代码示例

本文整理汇总了C#中Microsoft.Framework.ConfigurationModel.Configuration.TryGet方法的典型用法代码示例。如果您正苦于以下问题:C# Configuration.TryGet方法的具体用法?C# Configuration.TryGet怎么用?C# Configuration.TryGet使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Microsoft.Framework.ConfigurationModel.Configuration的用法示例。


在下文中一共展示了Configuration.TryGet方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: ConfigureServices

        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddCaching();
            services.AddSession();

            services.AddMvc();
            services.AddSingleton<PassThroughAttribute>();
            services.AddSingleton<UserNameService>();
            services.AddTransient<ITestService, TestService>();

            services.ConfigureMvc(options =>
            {
                options.Filters.Add(typeof(PassThroughAttribute), order: 17);
                options.AddXmlDataContractSerializerFormatter();
                options.Filters.Add(new FormatFilterAttribute());
            });

#if DNX451
            // Fully-qualify configuration path to avoid issues in functional tests. Just "config.json" would be fine
            // but Configuration uses CallContextServiceLocator.Locator.ServiceProvider to get IApplicationEnvironment.
            // Functional tests update that service but not in the static provider.
            var applicationEnvironment = services.BuildServiceProvider().GetRequiredService<IApplicationEnvironment>();
            var configurationPath = Path.Combine(applicationEnvironment.ApplicationBasePath, "config.json");

            // Set up configuration sources.
            var configuration = new Configuration()
                .AddJsonFile(configurationPath)
                .AddEnvironmentVariables();
            string diSystem;
            if (configuration.TryGet("DependencyInjection", out diSystem) &&
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                _autoFac = true;
                services.ConfigureRazorViewEngine(options =>
                {
                    var expander = new LanguageViewLocationExpander(
                        context => context.HttpContext.Request.Query["language"]);
                    options.ViewLocationExpanders.Insert(0, expander);
                });

                // Create the autofac container
                var builder = new ContainerBuilder();

                // Create the container and use the default application services as a fallback
                AutofacRegistration.Populate(
                    builder,
                    services);

                builder.RegisterModule<MonitoringModule>();

                var container = builder.Build();

                return container.Resolve<IServiceProvider>();
            }
            else
#endif
            {
                return services.BuildServiceProvider();
            }
        }
开发者ID:AndersBillLinden,项目名称:Mvc,代码行数:60,代码来源:Startup.cs

示例2: TestConfig

        private TestConfig()
        {
            _cstr = Environment.GetEnvironmentVariable("CUSTOMCONNSTR_TestAccount");
            const string cliConfigPath = "App.config";
            const string vsConfigPath = "..\\..\\App.config";
            if (_cstr == null)
            {
                var configuration = new Configuration();
                if (File.Exists(cliConfigPath))
                {
                    configuration.AddXmlFile(cliConfigPath);
                }
                else if (File.Exists(vsConfigPath))
                {
                    configuration.AddXmlFile(vsConfigPath);
                }

                if (configuration.TryGet("TestAccount:ConnectionString", out _cstr))
                {
                    _cstr = _cstr.Trim();
                }
            }
        }
开发者ID:Nyaoso,项目名称:EntityFramework,代码行数:23,代码来源:TestConfig.cs

示例3: TestConfig

        public TestConfig()
        {
            _dataSource = Environment.GetEnvironmentVariable("PERFRUN_DataSource");
            RuntimeFlavor = getRuntimeFlavor();

            var resultsDirectory = Environment.GetEnvironmentVariable("PERFRUN_ResultsDirectory");
            ResultsDirectory = string.IsNullOrEmpty(resultsDirectory) ? ResultsDirectory : resultsDirectory;

            const string cliConfigPath = "LocalConfig.json";
            const string vsConfigPath = "..\\..\\LocalConfig.json";

            if (_dataSource != null)
            {
                RunPerfTests = true;
            }
            else
            {
                var configuration = new Configuration();
                if (File.Exists(cliConfigPath))
                {
                    configuration.AddJsonFile(cliConfigPath);
                }
                else if (File.Exists(vsConfigPath))
                {
                    configuration.AddJsonFile(vsConfigPath);
                }

                if (configuration.TryGet("Data:DefaultDataSource:DataSource", out _dataSource))
                {
                    _dataSource = _dataSource.Trim();
                    string runPerfTests;
                    configuration.TryGet("Data:RunPerfTests", out runPerfTests);
                    RunPerfTests = !string.IsNullOrEmpty(runPerfTests) && !runPerfTests.ToLower().Equals("false");
                }
            }
        }
开发者ID:thegido,项目名称:EntityFramework,代码行数:36,代码来源:TestConfig.cs

示例4: Configure

        public void Configure(IBuilder app)
        {
            app.UseFileServer();
#if NET45
            var configuration = new Configuration()
                                    .AddJsonFile(@"App_Data\config.json")
                                    .AddEnvironmentVariables();

            string diSystem;

            if (configuration.TryGet("DependencyInjection", out diSystem) && 
                diSystem.Equals("AutoFac", StringComparison.OrdinalIgnoreCase))
            {
                app.UseMiddleware<MonitoringMiddlware>();

                var services = new ServiceCollection();

                services.AddMvc();
                services.AddSingleton<PassThroughAttribute>();
                services.AddSingleton<UserNameService>();
                services.AddTransient<ITestService, TestService>();                
                services.Add(OptionsServices.GetDefaultServices());

                // Create the autofac container 
                ContainerBuilder builder = new ContainerBuilder();

                // Create the container and use the default application services as a fallback 
                AutofacRegistration.Populate(
                    builder,
                    services,
                    fallbackServiceProvider: app.ApplicationServices);

                builder.RegisterModule<MonitoringModule>();

                IContainer container = builder.Build();

                app.UseServices(container.Resolve<IServiceProvider>());
            }
            else
#endif
            {
                app.UseServices(services =>
                {
                    services.AddMvc();
                    services.AddSingleton<PassThroughAttribute>();
                    services.AddSingleton<UserNameService>();
                    services.AddTransient<ITestService, TestService>();
                });
            }

            app.UseMvc(routes =>
            {
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action}");

                routes.MapRoute(
                    "controllerActionRoute",
                    "{controller}/{action}",
                    new { controller = "Home", action = "Index" });

                routes.MapRoute(
                    "controllerRoute",
                    "{controller}",
                    new { controller = "Home" });
            });
        }
开发者ID:Nakro,项目名称:Mvc,代码行数:65,代码来源:Startup.cs

示例5: LoadAndCombineKeyValuePairsFromDifferentConfigurationSources

        public void LoadAndCombineKeyValuePairsFromDifferentConfigurationSources()
        {
            // Arrange
            var dic1 = new Dictionary<string, string>()
                {
                    {"Mem1:KeyInMem1", "ValueInMem1"}
                };
            var dic2 = new Dictionary<string, string>()
                {
                    {"Mem2:KeyInMem2", "ValueInMem2"}
                };
            var dic3 = new Dictionary<string, string>()
                {
                    {"Mem3:KeyInMem3", "ValueInMem3"}
                };
            var memConfigSrc1 = new MemoryConfigurationSource(dic1);
            var memConfigSrc2 = new MemoryConfigurationSource(dic2);
            var memConfigSrc3 = new MemoryConfigurationSource(dic3);

            var config = new Configuration();

            string memVal1, memVal2, memVal3;
            bool memRet1, memRet2, memRet3;

            // Act
            config.AddLoadedSource(memConfigSrc1);
            config.AddLoadedSource(memConfigSrc2);
            config.AddLoadedSource(memConfigSrc3);

            memRet1 = config.TryGet("mem1:keyinmem1", out memVal1);
            memRet2 = config.TryGet("Mem2:KeyInMem2", out memVal2);
            memRet3 = config.TryGet("MEM3:KEYINMEM3", out memVal3);

            // Assert
            Assert.Contains(memConfigSrc1, config.Sources);
            Assert.Contains(memConfigSrc2, config.Sources);
            Assert.Contains(memConfigSrc3, config.Sources);

            Assert.True(memRet1);
            Assert.True(memRet2);
            Assert.True(memRet3);

            Assert.Equal("ValueInMem1", memVal1);
            Assert.Equal("ValueInMem2", memVal2);
            Assert.Equal("ValueInMem3", memVal3);

            Assert.Equal("ValueInMem1", config.Get("mem1:keyinmem1"));
            Assert.Equal("ValueInMem2", config.Get("Mem2:KeyInMem2"));
            Assert.Equal("ValueInMem3", config.Get("MEM3:KEYINMEM3"));
            Assert.Null(config.Get("NotExist"));

            Assert.Equal("ValueInMem1", config["mem1:keyinmem1"]);
            Assert.Equal("ValueInMem2", config["Mem2:KeyInMem2"]);
            Assert.Equal("ValueInMem3", config["MEM3:KEYINMEM3"]);
            Assert.Null(config["NotExist"]);
        }
开发者ID:perezgb,项目名称:Configuration,代码行数:56,代码来源:ConfigurationTest.cs


注:本文中的Microsoft.Framework.ConfigurationModel.Configuration.TryGet方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。