Skip to content

Commit 7044688

Browse files
Merge pull request #100868 from CamSoper/app-config
Added .net core 3.0 to Azure App Config docs
2 parents e679840 + f35e0d3 commit 7044688

File tree

2 files changed

+138
-8
lines changed

2 files changed

+138
-8
lines changed

articles/azure-app-configuration/concept-disaster-recovery.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Your application loads its configuration from both the primary and secondary sto
2424

2525
Technically, your application isn't executing a failover. It's attempting to retrieve the same set of configuration data from two App Configuration stores simultaneously. Arrange your code so that it loads from the secondary store first and then the primary store. This approach ensures that the configuration data in the primary store takes precedence whenever it's available. The following code snippet shows how you can implement this arrangement in the .NET Core CLI:
2626

27+
#### [.NET Core 2.x](#tab/core2x)
28+
2729
```csharp
2830
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
2931
WebHost.CreateDefaultBuilder(args)
@@ -34,8 +36,24 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
3436
.AddAzureAppConfiguration(settings["ConnectionString_PrimaryStore"], optional: true);
3537
})
3638
.UseStartup<Startup>();
37-
}
39+
40+
```
41+
42+
#### [.NET Core 3.x](#tab/core3x)
43+
44+
```csharp
45+
public static IHostBuilder CreateHostBuilder(string[] args) =>
46+
Host.CreateDefaultBuilder(args)
47+
.ConfigureWebHostDefaults(webBuilder =>
48+
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
49+
{
50+
var settings = config.Build();
51+
config.AddAzureAppConfiguration(settings["ConnectionString_SecondaryStore"], optional: true)
52+
.AddAzureAppConfiguration(settings["ConnectionString_PrimaryStore"], optional: true);
53+
})
54+
.UseStartup<Startup>());
3855
```
56+
---
3957

4058
Notice the `optional` parameter passed into the `AddAzureAppConfiguration` function. When set to `true`, this parameter prevents the application from failing to continue if the function can't load configuration data.
4159

articles/azure-app-configuration/enable-dynamic-configuration-aspnet-core.md

Lines changed: 119 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,13 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
5050
1. Add a reference to the `Microsoft.Azure.AppConfiguration.AspNetCore` NuGet package by running the following command:
5151

5252
```CLI
53-
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore --version 2.0.0-preview-010060003-1250
53+
dotnet add package Microsoft.Azure.AppConfiguration.AspNetCore --version 3.0.0-preview-010560002-1165
5454
```
5555
5656
1. Open *Program.cs*, and update the `CreateWebHostBuilder` method to add the `config.AddAzureAppConfiguration()` method.
5757
58+
#### [.NET Core 2.x](#tab/core2x)
59+
5860
```csharp
5961
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
6062
WebHost.CreateDefaultBuilder(args)
@@ -76,6 +78,30 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
7678
.UseStartup<Startup>();
7779
```
7880
81+
#### [.NET Core 3.x](#tab/core3x)
82+
83+
```csharp
84+
public static IHostBuilder CreateHostBuilder(string[] args) =>
85+
Host.CreateDefaultBuilder(args)
86+
.ConfigureWebHostDefaults(webBuilder =>
87+
webBuilder.ConfigureAppConfiguration((hostingContext, config) =>
88+
{
89+
var settings = config.Build();
90+
config.AddAzureAppConfiguration(options =>
91+
{
92+
options.Connect(settings["ConnectionStrings:AppConfig"])
93+
.ConfigureRefresh(refresh =>
94+
{
95+
refresh.Register("TestApp:Settings:BackgroundColor")
96+
.Register("TestApp:Settings:FontColor")
97+
.Register("TestApp:Settings:Message");
98+
});
99+
});
100+
})
101+
.UseStartup<Startup>());
102+
```
103+
---
104+
79105
The `ConfigureRefresh` method is used to specify the settings used to update the configuration data with the App Configuration store when a refresh operation is triggered. In order to actually trigger a refresh operation, a refresh middleware needs to be configured for the application to refresh the configuration data when any change occurs.
80106
81107
2. Add a *Settings.cs* file that defines and implements a new `Settings` class.
@@ -93,32 +119,85 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
93119
}
94120
```
95121
96-
3. Open *Startup.cs*, and update the `ConfigureServices` method to bind configuration data to the `Settings` class.
122+
3. Open *Startup.cs*, and use `IServiceCollection.Configure<T>` in the `ConfigureServices` method to bind configuration data to the `Settings` class.
123+
124+
#### [.NET Core 2.x](#tab/core2x)
97125
98126
```csharp
99127
public void ConfigureServices(IServiceCollection services)
100128
{
101129
services.Configure<Settings>(Configuration.GetSection("TestApp:Settings"));
130+
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
131+
}
132+
```
133+
134+
#### [.NET Core 3.x](#tab/core3x)
135+
136+
```csharp
137+
public void ConfigureServices(IServiceCollection services)
138+
{
139+
services.Configure<Settings>(Configuration.GetSection("TestApp:Settings"));
140+
services.AddControllersWithViews();
141+
}
142+
```
143+
---
144+
145+
4. Update the `Configure` method, adding the `UseAzureAppConfiguration` middleware to allow the configuration settings registered for refresh to be updated while the ASP.NET Core web app continues to receive requests.
146+
147+
148+
#### [.NET Core 2.x](#tab/core2x)
149+
150+
```csharp
151+
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
152+
{
153+
app.UseAzureAppConfiguration();
102154
103155
services.Configure<CookiePolicyOptions>(options =>
104156
{
105157
options.CheckConsentNeeded = context => true;
106158
options.MinimumSameSitePolicy = SameSiteMode.None;
107159
});
108160
109-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
161+
app.UseMvc();
110162
}
111163
```
112164
113-
4. Update the `Configure` method to add a middleware to allow the configuration settings registered for refresh to be updated while the ASP.NET Core web app continues to receive requests.
165+
#### [.NET Core 3.x](#tab/core3x)
114166
115167
```csharp
116-
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
168+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
117169
{
118-
app.UseAzureAppConfiguration();
119-
app.UseMvc();
170+
if (env.IsDevelopment())
171+
{
172+
app.UseDeveloperExceptionPage();
173+
}
174+
else
175+
{
176+
app.UseExceptionHandler("/Home/Error");
177+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
178+
app.UseHsts();
179+
}
180+
181+
// Add the following line:
182+
app.UseAzureAppConfiguration();
183+
184+
app.UseHttpsRedirection();
185+
186+
app.UseStaticFiles();
187+
188+
app.UseRouting();
189+
190+
app.UseAuthorization();
191+
192+
app.UseEndpoints(endpoints =>
193+
{
194+
endpoints.MapControllerRoute(
195+
name: "default",
196+
pattern: "{controller=Home}/{action=Index}/{id?}");
197+
});
120198
}
121199
```
200+
---
122201
123202
The middleware uses the refresh configuration specified in the `AddAzureAppConfiguration` method in `Program.cs` to trigger a refresh for each request received by the ASP.NET Core web app. For each request, a refresh operation is triggered and the client library checks if the cached value for the registered configuration settings have expired. For the cached values that have expired, the values for the settings are updated with the App Configuration store, and the remaining values remain unchanged.
124203
@@ -135,6 +214,8 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
135214
136215
2. Update the `HomeController` class to receive `Settings` through dependency injection, and make use of its values.
137216
217+
#### [.NET Core 2.x](#tab/core2x)
218+
138219
```csharp
139220
public class HomeController : Controller
140221
{
@@ -156,6 +237,37 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
156237
}
157238
```
158239
240+
#### [.NET Core 3.x](#tab/core3x)
241+
242+
```csharp
243+
public class HomeController : Controller
244+
{
245+
private readonly Settings _settings;
246+
private readonly ILogger<HomeController> _logger;
247+
248+
public HomeController(ILogger<HomeController> logger, IOptionsSnapshot<Settings> settings)
249+
{
250+
_logger = logger;
251+
_settings = settings.Value;
252+
}
253+
254+
public IActionResult Index()
255+
{
256+
ViewData["BackgroundColor"] = _settings.BackgroundColor;
257+
ViewData["FontSize"] = _settings.FontSize;
258+
ViewData["FontColor"] = _settings.FontColor;
259+
ViewData["Message"] = _settings.Message;
260+
261+
return View();
262+
}
263+
264+
// ...
265+
}
266+
```
267+
---
268+
269+
270+
159271
3. Open *Index.cshtml* in the Views > Home directory, and replace its content with the following script:
160272
161273
```html

0 commit comments

Comments
 (0)