NET provides support for passing options via environment variables, appsettings.json, and XML files. as well as command line arguments. In short, each higher level overrides the settings in lower levels.
To add options:
- Create a public class and name it (we’ll use funcOptions)
- Add a string property called returnValue (or name it whatever you like) and set the property to some value (e.g., MyOptions)
- We’re going to override that value in applications.json
- Add a property in appsettings.json (e.g., retVal) and set that property to a different value than we did in step 2
It’s time to set up the startup configuration. When you are done, your Program.cs should look like this:
var host = new HostBuilder() .ConfigureFunctionsWebApplication() .ConfigureServices((context, services) => { services.AddApplicationInsightsTelemetryWorkerService() .ConfigureFunctionsApplicationInsights() .AddOptions<MyOptions> .BindConfiguration(""); }) .Build(); host.Run();
The next step is to inject IOptions<MyOptions> and in the constructor set a member variable.
The Run method will look like this:
[Function("Function1")] public IActionResult Run(HttpTrigger(AuthorizationLevel .Anonymous, "get", "post")] HttpRequest req) { return new OKObjectResult(options.value.retValue); }