Beispiel #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="builder"></param>
        /// <param name="jsonConfigFiles"></param>
        /// <returns></returns>
        public static Microsoft.AspNetCore.Hosting.IWebHostBuilder UseJsonFileConfig(this Microsoft.AspNetCore.Hosting.IWebHostBuilder builder, Func <Microsoft.AspNetCore.Hosting.WebHostBuilderContext, IEnumerable <FileInfo> > jsonConfigFiles = null)
        {
            if (jsonConfigFiles == null)
            {
                return(builder);
            }

            //config builder
            builder.ConfigureAppConfiguration((h, g) =>
            {
                var files = jsonConfigFiles?.Invoke(h);
                if (files.IsNotNullOrEmpty())
                {
                    foreach (var file in files)
                    {
                        if (file.Exists)
                        {
                            g.AddConfiguration(new ConfigurationBuilder().SetBasePath(h.HostingEnvironment.ContentRootPath).AddJsonFile(file.FullName, true, true).Build());
                        }
                        else
                        {
                            throw new System.IO.FileNotFoundException(string.Format("找不到文件{0}", file.FullName));
                        }
                    }
                }

                //if (File.Exists(Path.Combine(h.HostingEnvironment.ContentRootPath, "appsettings.json")))
                //    g.AddConfiguration(new ConfigurationBuilder().SetBasePath(h.HostingEnvironment.ContentRootPath).AddJsonFile("appsettings.json", true, true).Build());

                //if (File.Exists(Path.Combine(h.HostingEnvironment.ContentRootPath, $"appsettings.{h.HostingEnvironment.EnvironmentName}.json")))
                //    g.AddConfiguration(new ConfigurationBuilder().SetBasePath(h.HostingEnvironment.ContentRootPath).AddJsonFile($"appsettings.{h.HostingEnvironment.EnvironmentName}.json", true, true).Build());
            });

            return(builder);
        }
Beispiel #2
0
    public static IHostBuilder AddApollo(this IHostBuilder hostBuilder, string appId, string metaServer)
    {
        if (appId == null)
        {
            throw new ArgumentNullException(nameof(appId));
        }
        if (metaServer == null)
        {
            throw new ArgumentNullException(nameof(metaServer));
        }

        return(hostBuilder.ConfigureAppConfiguration((_, builder) => builder.AddApollo(appId, metaServer)));
    }
Beispiel #3
0
    public static IHostBuilder AddApollo(this IHostBuilder hostBuilder, Action <ApolloOptions> configure)
    {
        if (configure == null)
        {
            throw new ArgumentNullException(nameof(configure));
        }

        var options = new ApolloOptions();

        configure(options);

        return(hostBuilder.ConfigureAppConfiguration((_, builder) => builder.AddApollo(options)));
    }
Beispiel #4
0
 /// <param name="hostBuilder"></param>
 /// <param name="fromAppConfiguration">apollo配置源,false:环境变量、命令行之类;true:appsettings.json之类</param>
 /// <param name="key">apollo配置前缀</param>
 public static IHostBuilder AddApollo(this IHostBuilder hostBuilder, bool fromAppConfiguration = true, string key = "apollo") =>
 fromAppConfiguration
         ? hostBuilder.ConfigureAppConfiguration((_, builder) => builder.AddApollo(builder.Build().GetSection(key)))
         : hostBuilder.ConfigureAppConfiguration((context, builder) => builder.AddApollo(context.Configuration.GetSection(key)));