I am trying to call a webapi method from my quartz.net schedule job. I am not sure whether the way I am doing is right? Can anyone help if this is the right way or is there any better approach available?
MethodRepository.cs
public async Task<IEnumerable<ResultClass>> GetResult(string queryCriteria)
{
return await _httpClient.Get(queryCriteria);
}
Quartz job:
public async void Execute(IJobExecutionContext context)
{
var results= await _repo.GetResult();
}
generic Httpclient :
public async Task<IEnumerable<T>> Get(string queryCriteria)
{
_addressSuffix = _addressSuffix + queryCriteria;
var responseMessage = await _httpClient.GetAsync(_addressSuffix);
responseMessage.EnsureSuccessStatusCode();
return await responseMessage.Content.ReadAsAsync<IEnumerable<T>>();
}
But the quartz documentation says I can't use async method in a quartz job. How can one the Web API method then?
Can I change the quartz job execute method as:
public void Execute(IJobExecutionContext context)
{
var result = _repo.GetResult().Result;
}
Quartz.NET 3.0 supports async/await out of the box. So you can (and must) now declare Execute method as Task returning and you can use async/await.
public async Task Execute(IJobExecutionContext context)
{
var result = await _repo.GetResult();
}