Home > Software engineering >  Debug Azure Function locally with Visual Studio 2019
Debug Azure Function locally with Visual Studio 2019

Time:12-07

I want to run a task every day at 5 am to write some data to the database to create some reports. For that I thought I could use Azure Functions (Timer trigger). My goal is to first debug the function locally (using a local database) before publishing it to Azure. For this I have created a new Azure Functions project in Visual Studio 2019 with the following parameters:

  • .NET 5.0 (Isolated).
  • Timer trigger.
  • Storage Account (AzureWebJobsStorage): Storage Emulator.
  • Schedule: 0 0 5 * * *

If I try to run the function (pressing F5 in the keyboard) without any changes to the code, it opens a CMD window with the colored Azure Functions logo created with characters and then the following error:

Error: unknown argument --port

In project properties > Debug tab > Application arguments I have --port 7282. I have a systray icon with this message: "Storage emulator is started".

I tried the answer of What is the simplest way to run a timer-triggered Azure Function locally once? question, but I get the same error.

What do I have to do to debug the function locally? Do I need to install any specific tool?


If it helps, I have the following files:

Program.cs

public class Program
{
    public static void Main()
    {
        var host = new HostBuilder()
            .ConfigureFunctionsWorkerDefaults()
            .Build();

        host.Run();
    }
}

Function1.cs:

public class Function1
{
    private readonly ILogger _logger;

    public Function1(ILoggerFactory loggerFactory)
    {
        _logger = loggerFactory.CreateLogger<Function1>();
    }

    [Function("Function1")]
    public void Run([TimerTrigger("0 0 5 * * *")] MyInfo myTimer)
    {
        _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

public class MyInfo { ... }
public class MyScheduleStatus { ... }

CodePudding user response:

Why do is port sat ?

What happens if you remove the --port (can't comment due to my low rep. There for I try to give the answer)

Then try to add RunOnStartup=true to your Function parameter as such:

[Function("Function1")]
    public void Run([TimerTrigger("0 0 5 * * *") RunOnStartup=true] MyInfo myTimer)
    {
        _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }

That should trigger it to run at start up for your application.

Have you looked at the MS Learn docs

  • Related