计划的 .NET WebJob V3 示例

Scheduled .NET WebJob V3 example

我已将我的 .NET(不是 .NET Core)WebJob 从 V2(运行良好)升级到 V3。我无法将其发送到 运行。我只希望 webjob 调用我根据此 CRON 计划编写的函数:“0 0 8,10,12,14,16,18,20 * * *”。 运行 的网站也是 .NET,而不是 .NET Core。

我该怎么做?我只想要一个简单的工作 .NET 代码示例。我看过这个问题 and this example https://github.com/Azure/azure-webjobs-sdk/blob/00686a5ae3b31ca1c70b477c1ca828e4aa754340/sample/SampleHost/Program.cs and this documentation https://docs.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#triggers,但其中 none 很有帮助。

Scheduled .NET WebJob V3 example

不,不可能。

WebJob3.x只支持.NET Core。这里是article

这里有一个关于Webjob 3.x for .net core 做一些设置。

实际上.Net webjob 或.Net Core webjob 的使用几乎相同,因为3.0 sdk 目标.NET 标准2.0。我用 Microsoft.Azure.WebJobs -version 3.0.4Microsoft.Azure.WebJobs.Extensions -version 3.0.1 进行了测试,我认为您的 TimerTrigger 不起作用,因为您丢失了对 AddTimers 扩展方法的调用。您可以在此处找到说明:Binding types.

我使用的其他包:

Microsoft.Extensions.Logging -version 2.2.0
Microsoft.Extensions.Logging.Console -version 2.2.0

这是我的main方法:

using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
namespace ConsoleApp20
{
class Program
{
    static void Main(string[] args)
    {
        var builder = new HostBuilder();
        builder.ConfigureWebJobs(b =>
        {
            b.AddAzureStorageCoreServices();
            b.AddTimers();
        });
        builder.ConfigureLogging((context, b) =>
        {
            b.AddConsole();

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

这是我的 Functions.cs:

public static void Run([TimerTrigger("0 0 8,10,12,14,16,18,20 * * *")]TimerInfo myTimer, ILogger log)
    {

        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

并使用 appsettings.json(不要忘记将 Copy to Output Directory 设置为 Copy always)来配置存储连接字符串。

结果如下: