Skip to content

Commit d4a597d

Browse files
author
Cam Soper
committed
added .net core 3.0
1 parent 205bea2 commit d4a597d

File tree

2 files changed

+132
-9
lines changed

2 files changed

+132
-9
lines changed

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

Lines changed: 18 additions & 0 deletions
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)
@@ -37,6 +39,22 @@ public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
3739
}
3840
```
3941

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>());
55+
```
56+
---
57+
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

4260
## Synchronization between configuration stores

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

Lines changed: 114 additions & 9 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,24 +119,33 @@ 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+
```
102133
103-
services.Configure<CookiePolicyOptions>(options =>
104-
{
105-
options.CheckConsentNeeded = context => true;
106-
options.MinimumSameSitePolicy = SameSiteMode.None;
107-
});
134+
#### [.NET Core 3.x](#tab/core3x)
108135
109-
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
136+
```csharp
137+
public void ConfigureServices(IServiceCollection services)
138+
{
139+
services.Configure<Settings>(Configuration.GetSection("TestApp:Settings"));
140+
services.AddControllersWithViews();
110141
}
111142
```
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.
112146
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.
147+
148+
#### [.NET Core 2.x](#tab/core2x)
114149
115150
```csharp
116151
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@@ -119,6 +154,43 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
119154
app.UseMvc();
120155
}
121156
```
157+
158+
#### [.NET Core 3.x](#tab/core3x)
159+
160+
```csharp
161+
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
162+
{
163+
if (env.IsDevelopment())
164+
{
165+
app.UseDeveloperExceptionPage();
166+
}
167+
else
168+
{
169+
app.UseExceptionHandler("/Home/Error");
170+
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
171+
app.UseHsts();
172+
}
173+
174+
// Add the following line:
175+
app.UseAzureAppConfiguration();
176+
177+
app.UseHttpsRedirection();
178+
179+
app.UseStaticFiles();
180+
181+
app.UseRouting();
182+
183+
app.UseAuthorization();
184+
185+
app.UseEndpoints(endpoints =>
186+
{
187+
endpoints.MapControllerRoute(
188+
name: "default",
189+
pattern: "{controller=Home}/{action=Index}/{id?}");
190+
});
191+
}
192+
```
193+
---
122194
123195
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.
124196
@@ -135,6 +207,8 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
135207
136208
2. Update the `HomeController` class to receive `Settings` through dependency injection, and make use of its values.
137209
210+
#### [.NET Core 2.x](#tab/core2x)
211+
138212
```csharp
139213
public class HomeController : Controller
140214
{
@@ -156,6 +230,37 @@ Before you continue, finish [Create an ASP.NET Core app with App Configuration](
156230
}
157231
```
158232
233+
#### [.NET Core 3.x](#tab/core3x)
234+
235+
```csharp
236+
public class HomeController : Controller
237+
{
238+
private readonly Settings _settings;
239+
private readonly ILogger<HomeController> _logger;
240+
241+
public HomeController(ILogger<HomeController> logger, IOptionsSnapshot<Settings> settings)
242+
{
243+
_logger = logger;
244+
_settings = settings.Value;
245+
}
246+
247+
public IActionResult Index()
248+
{
249+
ViewData["BackgroundColor"] = _settings.BackgroundColor;
250+
ViewData["FontSize"] = _settings.FontSize;
251+
ViewData["FontColor"] = _settings.FontColor;
252+
ViewData["Message"] = _settings.Message;
253+
254+
return View();
255+
}
256+
257+
// ...
258+
}
259+
```
260+
---
261+
262+
263+
159264
3. Open *Index.cshtml* in the Views > Home directory, and replace its content with the following script:
160265
161266
```html

0 commit comments

Comments
 (0)