5

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://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-how-to#triggers but none of it is helpful.

nmit026
  • 3,024
  • 2
  • 27
  • 53

2 Answers2

7

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:

enter image description here

George Chen
  • 13,703
  • 2
  • 11
  • 26
  • 4
    Perfect answer. The documentation for WebJobs and the blurry line between .NET and .NET Core (and .NET Standard) in V3 is so confusing and weird. This will help everyone get up and running quickly. Thanks! – nmit026 Jul 30 '19 at 23:38
  • Just out of interest, are you also getting this warning in the output: `warn: Host.Startup[0] Warning: Only got partial types from assembly: Microsoft.Azure.WebJobs.Extensions.Storage, Version=3.0.6.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35` I see it's an open issue on GitHub: https://github.com/Azure/azure-webjobs-sdk/issues/2060 – nmit026 Jul 30 '19 at 23:40
  • 1
    No I didn't get this issue. – George Chen Jul 31 '19 at 01:10
  • weird thing is happening with me when I set the cron expression for more then few seconds the Run function run only if the appsettings is not valid (or not existing), but when i set the appsettings with whatever cron expression the Run function is never called! any idea why? – Pistachio Nov 23 '22 at 20:39
0

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.

Joey Cai
  • 18,968
  • 1
  • 20
  • 30
  • 1
    That's not correct, V3 also supports .NET (although I had to actually talk to a Microsoft employee on GitHub to confirm it, after which they updated the documentation here: https://learn.microsoft.com/en-us/azure/app-service/webjobs-sdk-get-started) – nmit026 Jul 30 '19 at 23:43