1
Hello,
I am doing a project in ASP.NET MVC and wanted to use an asynchronous task. In my stock I have:
public async Task GerarItens(int codigo)
{
using (var dbTran = dbContext.Database.BeginTransaction())
{
try
{
>> VARIOS SELECTs, UPDATEs, INSERTs <<
dbTran.Commit;
await Task.FromResult<object>(null);
}
catch (Exception)
{
dbTran.Rollback();
throw;
}
}
}
On my controller I have:
private async Task GeradorItens(int codigo)
{
RepoExemplo repo = new RepoExemplo();
await repo.GerarItens(codigo);
}
public async Task<ActionResult> Listagem(int codigo)
{
try
{
RepoExemplo repo = new RepoExemplo();
await GeradorItens(codigo);
return View(repo.Listar(codigo));
}
catch (Exception ex)
{
return View("Error", ex.Message);
}
}
Doubts:
- I can use the
dbTranwithin aasynctask?When I use the
await? In myGerarItensofRepositorio, I would use in all mySelects,Inserts,Updates?2a. If case, example, I need to make a
selectafter ainsert, only in thatinserti put aawait, I’ll get the right result on myselect?2b. What I understood on the subject (speaking very layily), the
awaitago the task in the background, so in my example if theinserttake long to be done, myselectwill not perform with the information that thisinsertwould generate, correct?What return options do I have for a method
TASK? I used theawait Task.FromResult<object>(null);because I didn’t know what to put...public async Task<ActionResult> Listagem, it is correct to use this for what I am wanting to do?