Samuel
Samuel

Reputation: 68

How to bind trigger parameters to appsettings.json under Isolated azure function

When running under the in-process function model, you could resolve trigger parameters from the appsettings.json - assuming its been configured as a custom configuration source as defined here.

e.g. The below timertrigger would resolve ScheduleTriggerTime from the appsettings.json file.

[FunctionName("Function1")]
public async Task Run([TimerTrigger("%ScheduleTriggerTime%")]TimerInfo myTimer)

After moving to the isolated worker model, binding from appsettings.json no longer seems to work, I can only get it to work if the setting is in the local.settings.json file.

Example code below running under the isolated model, adding appsettings.json files to the configuration, and trying to get the trigger setting ScheduleTriggerTime from said appsettings files for the TimerTrigger. Upon starting the app the error "Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.Function1'. Microsoft.Azure.WebJobs.Host: '%ScheduleTriggerTime%' does not resolve to a value." is thrown.

Program.cs

using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

var host = new HostBuilder()
    .ConfigureFunctionsWebApplication()
        .ConfigureAppConfiguration((hostContext, configurationBuilder) =>
        {
            configurationBuilder
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile($"appsettings.json")
                .AddJsonFile($"appsettings.{hostContext.HostingEnvironment.EnvironmentName}.json", optional: true)
                .AddEnvironmentVariables();
        })
    .Build();

host.Run();

Function1.cs

using Microsoft.Azure.Functions.Worker;

namespace FunctionApp4
{
    public class Function1
    {
        [Function("Function1")]
        public void Run([TimerTrigger("%ScheduleTriggerTime%")] TimerInfo myTimer)
        {
        }
    }
}

appsettings.json

{
  "ScheduleTriggerTime": "0 */1 * * * *"
}

Is this an intentional change under the isolated model, or am I missing something to make this resolve the setting from the appsettings.json file?

Upvotes: 1

Views: 417

Answers (1)

Pravallika KV
Pravallika KV

Reputation: 8744

Set the schedule expression as an app setting in local.settings.json and use this property in function code by wrapping with % sign i.e., %ScheduleTriggerTime%.

  • Created a .NET 8.0 Isolated Azure function and configured scheduleTime application setting as shown below:

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated",
    "scheduledtime": "0 */5 * * * *"
  }
}

Function Code:

[Function("Function1")]
public void Run([TimerTrigger("%scheduledtime%")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
    if (myTimer.ScheduleStatus is not null)
    {
    _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

enter image description here

Response:

[2024-07-12T08:36:56.883Z] Azure Functions .NET Worker (PID: 4276) initialized in debug mode. Waiting for debugger to attach...
[2024-07-12T08:36:56.964Z] Worker process started and initialized.

Functions:

        Function1: timerTrigger

For detailed output, run func with --verbose flag.
[2024-07-12T08:37:01.933Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2024-07-12T08:40:00.087Z] Executing 'Functions.Function1' (Reason='Timer fired at 2024-07-12T14:10:00.0501505+05:30', Id=7a8cadcc-50fb-45e2-8f82-73188095026d)
[2024-07-12T08:40:08.505Z] C# Timer trigger function executed at: 7/12/2024 2:10:08 PM
[2024-07-12T08:40:08.505Z] Next timer schedule at: 7/12/2024 2:10:00 PM
[2024-07-12T08:40:08.534Z] Executed 'Functions.Function1' (Succeeded, Id=7a8cadcc-50fb-45e2-8f82-73188095026d, Duration=8472ms)

Upvotes: -1

Related Questions