I am trying to Create an Azure WebJob that runs a function every x seconds. I am following the tutorials on the Microsoft website and this example of how to use TimerTrigger with WebJobs on Github. However when I run locally my methods in Functions.cs do not seem to be running(no logging and break points aren't hit).
Program.cs:
public class Program
{
static async Task Main()
{
var builder = new HostBuilder();
builder.ConfigureWebJobs(b =>
{
b.AddAzureStorageCoreServices();
});
var host = builder.Build();
using (host)
{
await host.RunAsync();
}
}
}
Functions.cs:
public class Functions
{
public static void TimerJob([TimerTrigger("00:00:03", RunOnStartup = true)] TimerInfo timer)
{
Console.WriteLine("Timer job fired!");
}
}
My only output in the debug console is:
Hosting environment: Production
Content root path: C:\Users\<blah>\<Project\bin\Debug\net472\
Here are my nuget packages and versions:
I am using .NET Framework 4.7.2
Finally, I figured out why our timed trigger wasn't firing in our webjob. After installing the following nuget packages,
I was able to get it to work using your function.cs
code in my .net framework 4.7.1 .
So To make our webjob with timer trigger work, we'll need to use 2.X or 3.X versions with the following packages:
Microsoft.Azure.WebJobs.Extensions -version 3.0.6
Microsoft.Azure.WebJobs.Extensions.Storage -version 3.0.1
Microsoft.Extensions.Logging -version 2.1.0
Microsoft.Extensions.Logging.Console -version 2.1.0
For reference check all the packages which i used in my environment :
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.Azure.KeyVault.Core" version="1.0.0" targetFramework="net472" />
<package id="Microsoft.Azure.WebJobs" version="3.0.14" targetFramework="net472" />
<package id="Microsoft.Azure.WebJobs.Core" version="3.0.14" targetFramework="net472" />
<package id="Microsoft.Azure.WebJobs.Extensions" version="3.0.6" targetFramework="net472" />
<package id="Microsoft.Azure.WebJobs.Extensions.Storage" version="3.0.1" targetFramework="net472" />
<package id="Microsoft.Azure.WebJobs.Host.Storage" version="3.0.14" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Abstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Binder" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.EnvironmentVariables" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.FileExtensions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Configuration.Json" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.DependencyInjection.Abstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Abstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileProviders.Physical" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.FileSystemGlobbing" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Hosting" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Hosting.Abstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Abstractions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Configuration" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Logging.Console" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Options" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Options.ConfigurationExtensions" version="2.1.0" targetFramework="net472" />
<package id="Microsoft.Extensions.Primitives" version="2.1.0" targetFramework="net472" />
<package id="ncrontab.signed" version="3.3.0" targetFramework="net472" />
<package id="Newtonsoft.Json" version="11.0.2" targetFramework="net472" />
<package id="System.Buffers" version="4.4.0" targetFramework="net472" />
<package id="System.ComponentModel.Annotations" version="4.4.0" targetFramework="net472" />
<package id="System.Diagnostics.TraceSource" version="4.3.0" targetFramework="net472" />
<package id="System.Memory" version="4.5.0" targetFramework="net472" />
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.0" targetFramework="net472" />
<package id="System.Threading.Tasks.Dataflow" version="4.8.0" targetFramework="net472" />
<package id="WindowsAzure.Storage" version="9.3.1" targetFramework="net472" />
Here is my Program.cs
where i have added AddTimers
extension method just after b.AddAzureStorageCoreServices();
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Hosting;
namespace usetimertrigger
{
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();
}
}
}
}
function.cs
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.Logging;
using System;
namespace usetimertrigger
{
public class Functions
{
public static void TimerJob([TimerTrigger("00:00:03", RunOnStartup = true)] TimerInfo timer)
{
Console.WriteLine("Ajay timer trigger fired!");
}
}
}
As mentioned in the MS DOC , added my storage account access key which is copied from Azure portal>storage account>access key
And appsettings.json
should be Copy to Output Directory to Copy always for configure our storage connection string as shown below.
Output:-
Before adding b.AddTimers();
After adding b.AddTimers();
Reference:
SO THREAD : Scheduled .NET WebJob V3 example