Skip to content

ZeeLyn/Extensions.Configuration.Consul

Repository files navigation

Extensions.Configuration.Consul

Configuration center based on consul.

Package & Status

Package NuGet
Extensions.Configuration.Consul NuGet package

Configuration

Hardcoded configuration

  public class Program
	{
		public static void Main(string[] args)
		{
			CreateWebHostBuilder(args).Build().Run();
		}
		public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
			WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
			{
				config.AddConsul("http://127.0.0.1:8500");
			}).UseStartup<Startup>();
	  }

Command line configuration

Command Describetion
consul-configuration-addr Consul agent address
consul-configuration-token ACL Token HTTP API
consul-configuration-dc Consul data center
consul-configuration-folder Prefix of key
  public class Program
	{
		public static void Main(string[] args)
		{
			CreateWebHostBuilder(args).Build().Run();
		}
		public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
			WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
			{
				config.AddConsul(args);
			}).UseStartup<Startup>();
	  }

Reload when modified

    public void ConfigureServices(IServiceCollection services)
		{
			services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

			services.AddConsulConfigurationCenter();

			services.AddOptions();
			services.Configure<Configs>(Configuration.GetSection("TestConfig"));
		
		}

Usage

InstancePerLifetimeScope

  public class LibClass
	{
		private Configs Config;
		public LibClass(IOptionsSnapshot<Configs> config)
		{
			Config = config.Value;
		}

		public Configs Get()
		{
			return Config;
		}
	}

SingleInstance

  public class SingleClass
	{
		private Configs Config;

		public SingleClass(IOptionsMonitor<Configs> config)
		{
			Config = config.CurrentValue;
			config.OnChange(conf =>
			{
				Config = conf;
			});
		}

		public Configs Get()
		{
			return Config;
		}
	}

About

Configuration with consul key/value

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages