Scheduled .NET WebJob V3 example

↘锁芯ラ 提交于 2019-12-02 10:28:33

问题


I've upgraded my .NET (not .NET Core) WebJob from V2 (which was working fine) to V3. I'm having trouble getting it to run. I just want the webjob to call a function I've written according to this CRON schedule: "0 0 8,10,12,14,16,18,20 * * *". The website it's running with is .NET also, not .NET Core.

How do I do this? I just want a simple working .NET code sample. I've seen this question New Azure WebJob Project - JobHostConfiguration/RunAndBlock missing after NuGet updates 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 but none of it is helpful.


回答1:


Actually the use of .Net webjob or .Net Core webjob are almost same, cause the 3.0 sdk targets .NET standard 2.0. I test with Microsoft.Azure.WebJobs -version 3.0.4 and Microsoft.Azure.WebJobs.Extensions -version 3.0.1, i think your TimerTrigger doesn't work cause you lost call the AddTimers extension methods. You could find the description here:Binding types.

Other package I use:

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

This is my main method:

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();
        }
    }
}
}

This is my 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}");
    }

And use a appsettings.json(Don't forget set the Copy to Output Directory to Copy always) to configure the storage connection string.

Here is the result:




回答2:


Scheduled .NET WebJob V3 example

No, it is not possible.

WebJob 3.x only support for .NET Core. Here is the article.

Here is a SO thread about Webjob 3.x for .net core to do some settings.



来源:https://stackoverflow.com/questions/57264806/scheduled-net-webjob-v3-example

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!