I am using .net core 6 WebJob SDK Version 4.0.1:
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions"Version="4.0.1" />
I added the following line to my webjob setup code:
builder.ConfigureServices(s => s.AddSingleton<MyClass>());
I have a timer trigger like this:
public class TimerFunctions
{
public void TimerTriggerTest([TimerTrigger("*/5 * * * * *")] TimerInfo myTimer,
ILogger logger,
MyClass diTest
)
{
logger.LogInformation("TimerTrigger");
}
}
When run my WebJob project locally, I get the following error:
System.InvalidOperationException: Cannot bind parameter 'diTest' to type MyClass. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
Yes, the TimerTrigger in the Azure WebJobs SDK does support Dependency Injection (DI). You can use DI to inject dependencies into your timer trigger function by using the
[Inject]
attribute.For example, you could use DI to inject a service or repository into your timer trigger function, like this: