I have written a azure webjob which has timerTrigger. The static method gets called every minute and it has to make http POST request. I want to access the appSettings.json file within the timerTrigger static method. How can I access it?
I see examples where static method for QueueTrigger have ExecutionContext and TextWriter as parameters. Please see below example:
public static void ProcessOrder(
[QueueTrigger("orders")] Order order,
TextWriter log,
ExecutionContext context)
{
log.WriteLine("InvocationId: {0}", context.InvocationId);
}
How can I inject similar current execution context and TextWriter logger into my timerTrigger static method - "TimerTick"? Below is my TimerTrigger static method :
public class Test
{
private static IConfiguration _config;
private static IHttpHandler _httpHandler;
public Test(IConfiguration configuration, IHttpHandler httpHandler)
{
_config = configuration;
_httpHandler = httpHandler;
}
[Singleton]
public static void TimerTick([TimerTrigger("0 */1 * * * *")]TimerInfo myTimer)
{
string baseUrl = _config?.GetSection("WebJobConfiguration:url")?.Value;
string API = _config.GetSection("WebJobConfiguration:API")?.Value;
Console.WriteLine("URL: " + baseUrl + API);
_httpHandler.PostAsync(baseUrl + API, null);
}
}
======================================================================= Updating the question:
I updated the method which has TimerTrigger to be:
public async static Task TriggerNotification([TimerTrigger("%Job%")]TimerInfo myTimer, ExecutionContext context)
{....}
Reading the %Job% from config file using NameResolver.
When I try to pass the current execution context within TriggerNotification method, I get the following error:
How can I solve this?
The webjob is configured using HostBuilder. Below is the code. I'm using Azure webjob version 3.0.3.
static void Main(string[] args)
{
try
{
var builder = new HostBuilder()
.ConfigureAppConfiguration(SetupConfiguration)
.ConfigureLogging(SetupLogging)
.ConfigureServices(SetupServices)
.ConfigureWebJobs(webJobConfiguration =>
{
webJobConfiguration.AddTimers();
webJobConfiguration.AddAzureStorageCoreServices();
})
.UseSerilog()
.Build();
builder.Run();
} catch { ... }
}