How to synchronously call the asynchronization method from the quartz schedule task

I am trying to call the webapi method from my quartz.net schedule job. I'm not sure if I am doing this right? Can someone help if this is the right way or is there a better approach?

MethodRepository.cs

public async Task<IEnumerable<ResultClass>> GetResult(string queryCriteria) { return await _httpClient.Get(queryCriteria); } 

Quartz quest:

 public async void Execute(IJobExecutionContext context) { var results= await _repo.GetResult(); } 

shared 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 that I cannot use the asynchronous input method in the quartz job. How can I use the Web API method?

Can I change the quartz task execution method as:

 public void Execute(IJobExecutionContext context) { var result = _repo.GetResult().Result; } 
+5
source share
2 answers

If you need to do this, then yes, you can do it, but it will block the calling thread until the asynchronous operation completes.

Task.Result will turn any exception into an AggregateException.

Therefore, you should probably put your httpclient call in a try catch.

  try { var result = _repo.GetResult().Result; } catch (AggregateException ae) { // handle exception } 

Also, it looks like they are working on AsyncJob .

+4
source

Quartz.NET 3.0 supports async / await out of the box. That way, you can (and should) now declare the Execute method as a return task, and you can use async / wait.

 public async Task Execute(IJobExecutionContext context) { var result = await _repo.GetResult(); } 
+7
source

All Articles