【问题标题】:Debug Azure Function locally with Visual Studio 2019使用 Visual Studio 2019 在本地调试 Azure Function
【发布时间】:2022-12-04 10:50:35
【问题描述】:

我想每天早上 5 点运行一个任务,将一些数据写入数据库以创建一些报告。为此,我认为我可以使用Azure Functions(计时器触发器).我的目标是先在本地调试函数(使用本地数据库),然后再将其发布到 Azure。为此,我在中创建了一个新的 Azure Functions 项目视觉工作室 2019使用以下参数:

  • .NET 5.0(已隔离)。
  • 定时器触发器。
  • 存储帐户 (AzureWebJobsStorage):存储模拟器。
  • 时间表:0 0 5 * * *

如果我尝试在不对代码进行任何更改的情况下运行该函数(在键盘上按 F5),它会打开一个 CMD 窗口,其中包含用字符创建的彩色 Azure Functions 徽标,然后出现以下错误:

错误:未知参数 --port

在项目属性 > 调试选项卡 > 应用程序参数中,我有 --port 7282。我有一个带有此消息的系统托盘图标:“存储模拟器已启动”。

我尝试了What is the simplest way to run a timer-triggered Azure Function locally once?问题的答案,但我得到了同样的错误。

我需要做什么才能在本地调试该功能?我需要安装任何特定工具吗?


如果有帮助,我有以下文件:

程序.cs

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

        host.Run();
    }
}

功能1.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 { ... }

【问题讨论】:

    标签: c# azure debugging azure-functions visual-studio-2019


    【解决方案1】:

    为什么端口 sat ?

    如果你删除 --port 会发生什么(由于我的低代表无法评论。我试着给出答案)

    然后尝试将 RunOnStartup=true 添加到您的 Function 参数中,如下所示:

    [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}");
        }
    

    这应该会触发它在您的应用程序启动时运行。

    你看过MS Learn docs了吗

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-10-01
      • 1970-01-01
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2012-03-17
      • 2021-03-22
      • 1970-01-01
      相关资源
      最近更新 更多