Skip to content

Commit 968aa57

Browse files
committed
Upgrade
1 parent a1160c3 commit 968aa57

15 files changed

+482
-296
lines changed

Example/Example.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
<Project Sdk="Microsoft.NET.Sdk.Web">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp2.1</TargetFramework>
4+
<TargetFramework>netcoreapp2.2</TargetFramework>
55
</PropertyGroup>
66

77
<ItemGroup>
88
<Folder Include="wwwroot\" />
99
</ItemGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Autofac" Version="4.8.1" />
13-
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.3.0" />
12+
<PackageReference Include="Autofac" Version="4.9.1" />
13+
<PackageReference Include="Autofac.Extensions.DependencyInjection" Version="4.3.1" />
1414
<PackageReference Include="Consul" Version="0.7.2.6" />
1515
<PackageReference Include="Microsoft.AspNetCore.App" />
16-
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.1.1" />
16+
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="2.2.2" />
1717
</ItemGroup>
1818

1919
<ItemGroup>

Example/Program.cs

Lines changed: 18 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,27 @@
1-
using System;
2-
using System.IO;
3-
using Consul;
1+
using System.IO;
42
using Extensions.Configuration.Consul;
53
using Microsoft.AspNetCore;
64
using Microsoft.AspNetCore.Hosting;
75
using Microsoft.Extensions.Configuration;
86

97
namespace Example
108
{
11-
public class Program
12-
{
13-
public static void Main(string[] args)
14-
{
15-
CreateWebHostBuilder(args).Build().Run();
16-
ConsulConfigurationExtensions.Shutdown();
17-
}
18-
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
9+
public class Program
10+
{
11+
public static void Main(string[] args)
12+
{
13+
CreateWebHostBuilder(args).Build().Run();
14+
}
15+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
1916

20-
WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
21-
{
22-
config.SetBasePath(Directory.GetCurrentDirectory());
23-
config.AddCommandLine(args);
24-
config.AddJsonFile("appsettings.json", false, true);
25-
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", false, true);
26-
config.AddConsul(new ConsulClientConfiguration
27-
{
28-
Address = new Uri("http://192.168.1.142:8500")
29-
}, new ConsulQueryOptions
30-
{
31-
Prefix = "AppSetting/",
32-
TrimPrefix = true
33-
}, true);
34-
}).UseStartup<Startup>();
35-
}
17+
WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
18+
{
19+
config.SetBasePath(Directory.GetCurrentDirectory());
20+
config.AddJsonFile("appsettings.json", false, true);
21+
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", false, true);
22+
//config.AddConsul("http://192.168.1.133:8500");
23+
config.AddConsul(args);
24+
config.AddCommandLine(args);
25+
}).UseStartup<Startup>();
26+
}
3627
}

Example/Startup.cs

Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Autofac;
66
using Autofac.Extensions.DependencyInjection;
77
using Consul;
8+
using Extensions.Configuration.Consul;
89
using Microsoft.AspNetCore.Builder;
910
using Microsoft.AspNetCore.Hosting;
1011
using Microsoft.AspNetCore.Hosting.Server.Features;
@@ -17,41 +18,41 @@
1718

1819
namespace Example
1920
{
20-
public class Startup
21-
{
22-
public Startup(IConfiguration configuration)
23-
{
24-
Configuration = configuration;
25-
}
21+
public class Startup
22+
{
23+
public Startup(IConfiguration configuration)
24+
{
25+
Configuration = configuration;
26+
}
2627

27-
public IConfiguration Configuration { get; }
28-
public IContainer ApplicationContainer { get; private set; }
28+
public IConfiguration Configuration { get; }
29+
public IContainer ApplicationContainer { get; private set; }
2930

3031

31-
// This method gets called by the runtime. Use this method to add services to the container.
32-
public IServiceProvider ConfigureServices(IServiceCollection services)
33-
{
34-
services.AddOptions();
35-
services.Configure<Configs>(Configuration.GetSection("TestConfig"));
36-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
32+
// This method gets called by the runtime. Use this method to add services to the container.
33+
public IServiceProvider ConfigureServices(IServiceCollection services)
34+
{
35+
services.AddOptions();
36+
services.Configure<Configs>(Configuration.GetSection("TestConfig"));
37+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
38+
services.AddConsulConfigurationCenter();
39+
var builder = new ContainerBuilder();
40+
builder.Populate(services);
41+
builder.RegisterType<LibClass>().InstancePerLifetimeScope();
42+
builder.RegisterType<SingleClass>().SingleInstance();
43+
ApplicationContainer = builder.Build();
44+
return new AutofacServiceProvider(ApplicationContainer);
45+
}
3746

38-
var builder = new ContainerBuilder();
39-
builder.Populate(services);
40-
builder.RegisterType<LibClass>().InstancePerLifetimeScope();
41-
builder.RegisterType<SingleClass>().SingleInstance();
42-
ApplicationContainer = builder.Build();
43-
return new AutofacServiceProvider(ApplicationContainer);
44-
}
47+
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
48+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
49+
{
50+
if (env.IsDevelopment())
51+
{
52+
app.UseDeveloperExceptionPage();
53+
}
4554

46-
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
47-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
48-
{
49-
if (env.IsDevelopment())
50-
{
51-
app.UseDeveloperExceptionPage();
52-
}
53-
54-
app.UseMvc();
55-
}
56-
}
55+
app.UseMvc();
56+
}
57+
}
5758
}

Example/appsettings.Development.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"Logging": {
33
"LogLevel": {
4-
"Default": "Debug",
4+
"Default": "Trace",
55
"System": "Information",
66
"Microsoft": "Information"
77
}

README.md

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,50 +6,68 @@ Package | NuGet
66
---------|------
77
Extensions.Configuration.Consul|[![NuGet package](https://buildstats.info/nuget/Extensions.Configuration.Consul)](https://www.nuget.org/packages/Extensions.Configuration.Consul)
88

9-
# Usage
109

11-
### Register
1210

11+
# Configuration
12+
13+
## Hardcoded configuration
1314
```csharp
1415
public class Program
1516
{
1617
public static void Main(string[] args)
1718
{
1819
CreateWebHostBuilder(args).Build().Run();
19-
ConsulConfigurationExtensions.Shutdown();
2020
}
21+
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
22+
WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
23+
{
24+
config.AddConsul("http://127.0.0.1:8500");
25+
}).UseStartup<Startup>();
26+
}
27+
```
28+
29+
## Command line configuration
30+
Command | Describetion
31+
---------|------
32+
consul-configuration-addr|Consul agent address
33+
consul-configuration-token|ACL Token HTTP API
34+
consul-configuration-dc|Consul data center
35+
consul-configuration-folder|Prefix of key
36+
2137

2238

39+
```csharp
40+
public class Program
41+
{
42+
public static void Main(string[] args)
43+
{
44+
CreateWebHostBuilder(args).Build().Run();
45+
}
2346
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
2447
WebHost.CreateDefaultBuilder(args).ConfigureAppConfiguration((context, config) =>
2548
{
26-
config.SetBasePath(Directory.GetCurrentDirectory());
27-
config.AddJsonFile("appsettings.json", false, true);
28-
config.AddJsonFile($"appsettings.{context.HostingEnvironment.EnvironmentName}.json", false, true);
29-
config.AddConsul("http://192.168.1.142:8500", "", "AppSetting/", "dc1", true);
49+
config.AddConsul(args);
3050
}).UseStartup<Startup>();
3151
}
3252
```
3353

3454

55+
## Reload when modified
3556
```csharp
36-
public IServiceProvider ConfigureServices(IServiceCollection services)
57+
public void ConfigureServices(IServiceCollection services)
3758
{
59+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
60+
61+
services.AddConsulConfigurationCenter();
62+
3863
services.AddOptions();
3964
services.Configure<Configs>(Configuration.GetSection("TestConfig"));
40-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
41-
42-
var builder = new ContainerBuilder();
43-
builder.Populate(services);
44-
builder.RegisterType<LibClass>().InstancePerLifetimeScope();
45-
builder.RegisterType<SingleClass>().SingleInstance();
46-
ApplicationContainer = builder.Build();
47-
return new AutofacServiceProvider(ApplicationContainer);
65+
4866
}
4967
```
5068

51-
52-
### InstancePerLifetimeScope
69+
# Usage
70+
## InstancePerLifetimeScope
5371
```csharp
5472
public class LibClass
5573
{
@@ -66,7 +84,7 @@ Extensions.Configuration.Consul|[![NuGet package](https://buildstats.info/nuget/
6684
}
6785
```
6886

69-
### SingleInstance
87+
## SingleInstance
7088
```csharp
7189
public class SingleClass
7290
{

src/Extensions.Configuration.Consul/ConsulAgentConfiguration.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
namespace Extensions.Configuration.Consul
44
{
5-
public class ConsulAgentConfiguration
6-
{
7-
public ConsulClientConfiguration ClientConfiguration { get; set; }
5+
public class ConsulAgentConfiguration
6+
{
7+
public ConsulClientConfiguration ClientConfiguration { get; set; }
88

9-
public ConsulQueryOptions QueryOptions { get; set; }
10-
}
9+
public ConsulQueryOptions QueryOptions { get; set; }
10+
}
1111
}

src/Extensions.Configuration.Consul/ConsulConfigurationExtensions.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)