I have different projects, in some of them it is fairly simple to access configuration items, in others it’s not – and I have difficulties finding out why. All are .NET 6 projects.
Note I have checked out some questions that looked similar as well as MS documentation but neither seems to work. Kindly read through my question before flagging it as duplicate.
How it works in a Function App:
In my Startup.cs
file I have this:
public override void Configure(IFunctionsHostBuilder builder)
{
[...]
// This is not really relevant but you see how I access configuration settings
builder.Services.AddHttpClient("MyClient", client =>
{
client.BaseAddress = new Uri(C.Configuration.GetValue<string>("BaseAddress"));
client.DefaultRequestHeaders.Add("Accept", "application/json");
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {C.Configuration.GetValue<string>("ApiToken")}");
});
[...]
}
public override void ConfigureAppConfiguration(IFunctionsConfigurationBuilder builder)
{
builder.ConfigurationBuilder
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("local.settings.json", true)
.AddUserSecrets(Assembly.GetExecutingAssembly(), true)
.AddEnvironmentVariables()
.Build();
}
Also, I have a static class C
that holds all kinds of constants – as well as the configuration. I post the relevant part as a screenshot on purpose, you’ll understand why.
So far, so good – in any place of the whole Function App project I can access any configuration setting by using C.Configuration.GetValue<Type>("setting")
.
How it does not work in an App Service
Now I have an App Service – and all I want is the exactly same functionality. However, it does not work, Configuration
is null. The relevant part of C.cs
looks exactly the same:
You’ll notice, however, that in one case the line using Microsoft.Extensions.Configuration;
is greyed out by VS which means it is considered obsolete, while this is not the case in the Function App C.cs
.
I assumed this must have something to do with the startup configuration or dependency injection, so I tried to configure it the same way as in the Function App. This is what I added to my Program.cs
which obviously is the equivalent to Startup.cs
in a Function App:
builder.Configuration
.SetBasePath(Environment.CurrentDirectory)
.AddJsonFile("appsettings.json", true)
.AddUserSecrets(Assembly.GetExecutingAssembly(), true)
.AddEnvironmentVariables()
.Build();
ConfigurationManager configuration = builder.Configuration;
The configuration is even added as a DI Singleton:
builder.Services.AddSingleton<IConfiguration>(configuration);
Now you probably ask why I want to have the configuration in that C class when it’s there as an injectable singleton. From what I understand the singleton is injected in my Controller classes – but I have lots of other classes that need to get values from the configuration. I am perfectly aware that I could pass down the configuration object to all dependent classes as a parameter in the constructor. This concept of DI is absolutely legit, but I consider it a bit over the top when it comes to configuration items. Just calling
`C.Configuration.GetValue<Type>("setting")`
anywhere in the code seems more convenient and logical to me. Note that C is not a singleton class but rather a static class – and since this works in the Function App, I had hoped it would work in the App service project as well.
Anyone can point me in the right direction? Happy to share more code snippets if required.
2
Answers
These are some OAuth request samples. I think this will help you to get some idea about the OAuth request.
SynchParameterModel class
Are you ever actually calling
C.Configuration = XXX
anywhere? I see in your code screenshots that you’re using the configuration builder to setupIConfiguration
for dependency injection, but nowhere are you assigning it to your staticC.Configuration
property.Maybe it’s something as simple as