I have a web app running in Azure which is currently running on .NET 5, which I'm trying to upgrade to 6. I have refactored the code to remove the Startup.cs
file and refactored Program.cs
to suit the new structure of .NET 6 apps. Before anybody else tells me that the old way with two files still works, yes I know, but I want to move to the new standard so that this can serve as a template for future apps, which by default when first created in VS only use the one file.
In the existing Startup.cs
file, I have something like this:
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Identity.Web;
namespace MyApp.Web.Server
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(Configuration.GetSection(Constants.AzureAdB2C))
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { "https://example.com/api/query" })
.AddInMemoryTokenCaches();
// Other services added here
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
}
}
}
When the Startup
constructor is called, an IConfiguration
instance is injected in, which I understand is done dynamically when running on DevOps to include all app settings set in the App Service -> Settings -> Configuration -> Application settings page. This all works fine at the moment.
However, the new Program.cs
file with the .NET 6 syntax looks like this:
using System.Reflection;
using Microsoft.AspNetCore.Authentication.OpenIdConnect;
using Microsoft.Identity.Web;
IConfiguration configuration;
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
string appSettingsPath = Path.Combine(
Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),
"appsettings.json");
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile(appSettingsPath, optional: false, reloadOnChange: true)
.Build();
builder.Services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApp(configuration.GetSection(Constants.AzureAdB2C))
.EnableTokenAcquisitionToCallDownstreamApi(new string[] { "https://example.com/api/query" })
.AddInMemoryTokenCaches();
// Other services added here
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(
endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapFallbackToPage("/_Host");
});
app.Run();
As you can see above, there is no constructor and therefore I have no way of injecting the IConfiguration
instance, so at the moment I'm presuming there is an appsettings.json
file and creating it from that. This won't work when running in the Azure environment as the settings specified in the dashboard don't get added to the appsettings.json
file, they are set as environment variables.
I can retrieve environment variables but I can't retrieve variable groups, as is the requirement in this particular case of adding Microsoft.Identity
authentication. Is there no way of dynamically resolving/injecting the IConfiguration
instance as before?