Search code examples
azure.net-coreazure-functions

How to add a new function to a dotnet-isolated (net8.0) function app using Azure Functions Core Tools?


I want to add a new function to my dotnet-isolated (net8.0) function app project using Azure Functions Core Tools, but I'm getting the following error:

Intialize function app project:

func init --worker-runtime dotnet-isolated --target-framework net8.0 --docker

Add new function:

func new --language c# --template "Timer trigger" --name UpdateApplications
Selected language doesn't match worker set in local.settings.json.Selected worker is: dotnetIsolated and selected language is: dotnet

How can I add new C# functions to my function app?


Solution

  • To add a new function, remove --language c# in the command func new --language c# --template "Timer trigger" --name UpdateApplications.

    I have created the Timer Trigger .NET 8.0 isolated Azure function using below commands:

    1. func init --worker-runtime dotnet-isolated --target-framework net8.0 --docker
    2. func new --template "Timer trigger" --name UpdateApplications
    

    enter image description here

    enter image description here

    local.settings.json:

    {
        "IsEncrypted": false,
        "Values": {
            "AzureWebJobsStorage": "UseDevelopmentStorage=true",
            "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
        }
    }
    

    UpdateApplications.cs:

    public class UpdateApplications
        {
            private readonly ILogger _logger;
    
            public UpdateApplications(ILoggerFactory loggerFactory)
            {
                _logger = loggerFactory.CreateLogger<UpdateApplications>();
            }
    
            [Function("UpdateApplications")]
            public void Run([TimerTrigger("0 */5 * * * *")] 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}");
                }
            }
        }
    

    Program.cs:

    var host = new HostBuilder()
        .ConfigureFunctionsWebApplication()
        .ConfigureServices(services => {
            services.AddApplicationInsightsTelemetryWorkerService();
            services.ConfigureFunctionsApplicationInsights();
        })
        .Build();
    
    host.Run();
    

    Response:

    Functions:
    
            UpdateApplications: timerTrigger
    
    For detailed output, run func with --verbose flag.
    [2024-09-06T09:20:14.269Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
    [2024-09-06T09:25:00.107Z] Executing 'Functions.UpdateApplications' (Reason='Timer fired at 2024-09-06T14:55:00.0494065+05:30', Id=913778fb-b3a2-496b-a53a-158293318108)
    [2024-09-06T09:25:00.243Z] Next timer schedule at: 9/6/2024 2:55:00 PM
    [2024-09-06T09:25:00.243Z] C# Timer trigger function executed at: 9/6/2024 2:55:00 PM
    [2024-09-06T09:25:00.273Z] Executed 'Functions.UpdateApplications' (Succeeded, Id=913778fb-b3a2-496b-a53a-158293318108, Duration=211ms)