Azure WebJobs找不到功能

时间:2019-02-04 22:29:28

标签: c# .net azure azure-webjobs

Program.cs:

    public static void Main(string[] args)
    {
        var builder = new HostBuilder()
            .ConfigureLogging((context, b) =>
            {
                b.SetMinimumLevel(LogLevel.Debug);
                b.AddConsole();
            })
            .UseConsoleLifetime();

        var host = builder.Build();
        using (host)
        {
           host.Run();
        }
    }

Functions.cs:

public class Functions
{
    private readonly ISampleServiceA _sampleServiceA;
    private readonly ISampleServiceB _sampleServiceB;

    public Functions(ISampleServiceA sampleServiceA, ISampleServiceB sampleServiceB)
    {
        _sampleServiceA = sampleServiceA;
        _sampleServiceB = sampleServiceB;
    }

    public static void RunSomething([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer, ILogger log) // every one minute
    {
        if (myTimer.IsPastDue)
        {
            log.LogInformation("Timer is running late!");
        }
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

我跑步时得到:

dbug: Microsoft.Extensions.Hosting.Internal.Host[1]
      Hosting starting
dbug: Microsoft.Extensions.Hosting.Internal.Host[2]
      Hosting started

我已经阅读了几个示例,并且给人的印象是,通过创建新的控制台应用程序,引用所需的程序集并包括上述内容,WebJob应该“可以正常工作”。我是否省略了一些关键配置?

版本:

enter image description here

1 个答案:

答案 0 :(得分:2)

由于它是时间触发功能,因此您在配置中缺少AddTimers()。

请为您的项目安装软件包Microsoft.Azure.WebJobs.Extensions,然后在program.cs->主要方法中:将AddTimers添加到webjob配置中,如下所示:

                var builder = new HostBuilder()
                .ConfigureWebJobs(
                b =>
                {
                    b.AddTimers();
                    //other configure
                })
                .ConfigureLogging((context, b) =>
                {
                    b.SetMinimumLevel(LogLevel.Debug);
                    b.AddConsole();
                })
                // your other configure
                .UseConsoleLifetime();

您也可以参考我的previous answer来了解针对webjob 3.x的更多配置。