N
N
netcore2019-03-05 12:39:39
ASP.NET
netcore, 2019-03-05 12:39:39

How not to wait for await in asp net core?

Let's say there is a method

public async Task<ActionResult> Like(int postId)
{
     //получаем id юзера из клеймов
    await _likeFacade.LikeAsync(UserId,postId, nameOf(News));
    return NoContent();
}

It is not necessary to wait for the LikeAsync method to finish executing and return to the current context. In the background, without await, I can’t write a like to the database, when return NoContent() everything is disposed. .ConfigureAwait(false) doesn't seem to work as well either. The controller method is still waiting to be executed. Or am I doing something wrong and this is a bad practice, in which it is better to wait for execution and normally return 204?
UPD
Problem solved, DI was dying after return NoContent(); It was necessary to get it from HttpAccessor and wrap it in Task.Run + using

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Filatov, 2019-03-05
@NYMEZIDE

In the background, without await, I can’t write a like to the database, when return NoContent() everything is disposed.

therefore it is necessary either to write await or to do handles GetAwaiter().GetResult();
everything is correct, async/await is not parallelization of code execution.
you have it all right. a call is made, 204 is returned. There shouldn't be any problems.
the only thing is that you don't catch any exception if LikeAsync fails and don't handle it.
read more about async/await , I think you misunderstand this concept

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question