Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to synchronously call async method from quartz schedule job

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;
}
like image 526
Mukil Deepthi Avatar asked Jul 14 '16 15:07

Mukil Deepthi


People also ask

How do you sync async method?

The simplest way to execute a method asynchronously is to start executing the method by calling the delegate's BeginInvoke method, do some work on the main thread, and then call the delegate's EndInvoke method. EndInvoke might block the calling thread because it does not return until the asynchronous call completes.

Does async await make it synchronous?

The async keyword does not make code synchronous. async/await is just a tool to make the syntax for interacting with promises nicer. It's still using promises. "node is asynchronous (meaning multiple lines of code execute at the same time)" that is not what asynchronous means.

What happens when you call async method without await?

The call to the async method starts an asynchronous task. However, because no Await operator is applied, the program continues without waiting for the task to complete. In most cases, that behavior isn't expected.

Is await async the same as sync?

No it's not the same. Your async code block is waiting for the await call to return to continue, however the rest of your application isn't waiting and can still continue like normal.


2 Answers

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();
}
like image 149
Marko Lahma Avatar answered Sep 30 '22 23:09

Marko Lahma


If you have to do it - then yes you can do that, but it will block the calling thread until the asynchronous operation is complete.

Task.Result will wrap any exception into an AggregateException.

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

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

Also, it seems they are working on an AsyncJob.

like image 37
William Xifaras Avatar answered Sep 30 '22 21:09

William Xifaras