Skip to content

Commit

Permalink
more changes to azure app config connection
Browse files Browse the repository at this point in the history
hopefully this is more reliable
  • Loading branch information
NielsPilgaard committed Oct 17, 2023
1 parent 7174ec0 commit b00cdd2
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 37 deletions.
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace Jordnaer.Shared.Infrastructure;

public static class AzureAppConfigurationExtensions
{
public static WebApplicationBuilder AddAzureAppConfiguration(this WebApplicationBuilder builder, string configKey = "ConnectionStrings:AppConfig")
{
string? connectionString = builder.Configuration[configKey];
if (connectionString is null)
{
return builder.Environment.IsDevelopment()
? builder
: throw new InvalidOperationException("Connection string 'AppConfig' not found.");
}

builder.Services.AddAzureAppConfiguration();
builder.Configuration.AddAzureAppConfiguration(options =>
options.Connect(connectionString)
// Load all keys that have no label
.Select("*")
// Configure to reload builder if the registered sentinel key is modified
.ConfigureRefresh(refreshOptions =>
{
refreshOptions.Register("Sentinel", refreshAll: true);
refreshOptions.SetCacheExpiration(TimeSpan.FromMinutes(5));
})
.UseFeatureFlags(flagOptions => flagOptions.CacheExpirationInterval = TimeSpan.FromMinutes(3)));

return builder;
}
}
namespace Jordnaer.Chat;

public static class AzureAppConfigurationExtensions
{
public static WebApplicationBuilder AddAzureAppConfiguration(this WebApplicationBuilder builder)
{
string? connectionString = builder.Configuration.GetConnectionString("AppConfig") ?? builder.Configuration["appconfig"];
if (connectionString is null)
{
throw new InvalidOperationException("Failed to find connection string to Azure App Configuration. Keys checked: 'ConnectionStrings:AppConfig' and 'appconfig'");
}

builder.Services.AddAzureAppConfiguration();
builder.Configuration.AddAzureAppConfiguration(options =>
options.Connect(connectionString)
// Load all keys that have no label
.Select("*")
// Configure to reload builder if the registered sentinel key is modified
.ConfigureRefresh(refreshOptions =>
{
refreshOptions.Register("Sentinel", refreshAll: true);
refreshOptions.SetCacheExpiration(TimeSpan.FromMinutes(5));
})
.UseFeatureFlags(flagOptions => flagOptions.CacheExpirationInterval = TimeSpan.FromMinutes(3)));

return builder;
}
}
3 changes: 3 additions & 0 deletions src/container_apps/Jordnaer.Chat/Jordnaer.Chat.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@

<ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />

<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="8.1.1" />

<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="6.1.0" />

<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.12" />
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="7.0.12">
<PrivateAssets>all</PrivateAssets>
Expand Down
2 changes: 1 addition & 1 deletion src/container_apps/Jordnaer.Chat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
{
var builder = WebApplication.CreateBuilder(args);

builder.AddAzureAppConfiguration("appconfig");
builder.AddAzureAppConfiguration();

builder.AddSerilog();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="6.1.0" />

<PackageReference Include="Serilog.Exceptions" Version="8.4.0" />
<PackageReference Include="Serilog.AspNetCore" Version="7.0.0" />
<PackageReference Include="Serilog.Sinks.Grafana.Loki" Version="8.1.0" />
Expand Down
28 changes: 28 additions & 0 deletions src/web/Server/Extensions/AzureAppConfigurationExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Jordnaer.Server.Extensions;

public static class AzureAppConfigurationExtensions
{
public static WebApplicationBuilder AddAzureAppConfiguration(this WebApplicationBuilder builder)
{
string? connectionString = builder.Configuration.GetConnectionString("AppConfig");
if (connectionString is null)
{
throw new InvalidOperationException("Failed to find connection string to Azure App Configuration. Keys checked: 'ConnectionStrings:AppConfig'");
}

builder.Services.AddAzureAppConfiguration();
builder.Configuration.AddAzureAppConfiguration(options =>
options.Connect(connectionString)
// Load all keys that have no label
.Select("*")
// Configure to reload builder if the registered sentinel key is modified
.ConfigureRefresh(refreshOptions =>
{
refreshOptions.Register("Sentinel", refreshAll: true);
refreshOptions.SetCacheExpiration(TimeSpan.FromMinutes(5));
})
.UseFeatureFlags(flagOptions => flagOptions.CacheExpirationInterval = TimeSpan.FromMinutes(3)));

return builder;
}
}
2 changes: 2 additions & 0 deletions src/web/Server/Jordnaer.Server.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
<PackageReference Include="MassTransit.AspNetCore" Version="7.3.1" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="8.1.1" />

<PackageReference Include="Microsoft.Azure.AppConfiguration.AspNetCore" Version="6.1.0" />

<PackageReference Include="Microsoft.AspNetCore.Authentication.Facebook" Version="7.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.Google" Version="7.0.12" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.MicrosoftAccount" Version="7.0.12" />
Expand Down

0 comments on commit b00cdd2

Please sign in to comment.