2

I have seem examples online of developers using an appsettings.json file and injecting it like this in Program.cs

config.AddJsonFile("appsettings.json", optional: true);

My question is why? What is the benefit and purpose to doing this, when we already have host.json which seems to serve an identical purpose? I am just confused on what functionality this has over host.json.I am using .NET 8 v4 Isolated Functions if that matters.

4 Answers 4

3

The reason for having both appsettings.json and host.json in Azure Functions is to separate concerns between application configuration and function runtime configuration:

appsettings.json:

  • Used for application-specific settings.
  • Stores things like API keys, database connection strings, and custom app settings.
  • This file is part of .NET's configuration system, making it easier to manage app settings and environment-specific values (e.g., appsettings.Development.json, appsettings.Production.json).

host.json:

  • Used for Azure Functions runtime settings.
  • Configures things like timeouts, retries, and logging behavior.
  • This file is specifically for controlling how the Azure Functions runtime behaves, ensuring that the function host runs as expected.

Why two files?

Separation of concerns: appsettings.json is for your application’s needs, while host.json is for managing how Azure Functions runs in the cloud. Keeping them separate helps maintain cleaner and more organized configuration.

Sign up to request clarification or add additional context in comments.

Comments

0

It seems azure function only support key-value from local.settings.json file, the settings from normal appsettings.json may have some complex structure and function app cannot parse them. Function app won't read values from appsettings.json as environment variable. Haven't tested it.

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
-2

@user3010678

So, using appsettings.json in your Azure Functions app is like having a dedicated spot for all your application-specific settings. It's a comon practice in .NET to keep infra related stuff in host.json and application-specific configurations in appsettings.json.

Here is a sample snipp tp demonstrate

// Program.cs

var builder = WebApplication.CreateBuilder(args);

// Add configuration from appsettings.json
builder.Configuration.AddJsonFile("appsettings.json", optional: true);

// Access configuration settings
var mySetting = builder.Configuration["MySetting"];

// Now, you can use 'mySetting' in your app
// ...

var app = builder.Build();

// Configure the application
// ...

app.Run();

add the app.jspn like this

// appsettings.json

{
  "MySetting": "SomeValue",
  "AnotherSetting": {
    "SubSetting": "SubValue"
  }
}

If you can see above you can easily store your app-specific settings in appsettings.json and retrieve them in your code. Hope this helps you.

3 Comments

thanks for the answer. But I still dont understand I guess, why not put these values in host.json or local.settings.json? Can you explain more the difference between "Infrastructure" settings and "application" settings? Is logging considered application or infrastructure?
This is not the azure function way of doing things. Use the host.json and local.settings.json setup and configure deployed settings in the azure functions portal pag. learn.microsoft.com/en-us/azure/azure-functions/…
This code is for a web app project, not an AzureFunctions project. Function projects don't give you a WebApplicationBuilder, instead you only get IConfigurationBuilder, so you can't add files to Configuration...
-2

It's just bad form and you should not do it. These developers you see examples from seem to be used to developing web applications in MVC and the like, where the appsettings file is the central place to put the "settings" of an app.

The thing is. Azure functions have a very specific place to put these settings. Locally you can put it in local.settings.json. This file is only used on you local machine for debugging. It's ignored for git and not pushed to the cloud.

When deploying to a function app in the azure cloud, you can set these settings for the production environment through the azure portal for azure functions in the settings tab.

The limitation with doing it this way is that the azure function settings only supports name value pairs. No complex objects and such. A feature many developeres like from the appsettings way of doing things. However! To change this appsettings file you have to redeploy a function. If you use the settings stuff in the azure portal you don't have to do that and you can have slot specific settings and such. In my opinion (others may disagree) changing configuration should never require redeploying something.

for more reading:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-how-to-use-azure-function-app-settings?tabs=portal

1 Comment

If your Function App is deployed using ARM/Bicep templates, then re-deploying will clear out any application settings that were manually added in the portal. learn.microsoft.com/en-us/azure/azure-resource-manager/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.