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
dbTran
within aasync
task
?When I use the
await
? In myGerarItens
ofRepositorio
, I would use in all mySelects
,Inserts
,Updates
?2a. If case, example, I need to make a
select
after ainsert
, only in thatinsert
i put aawait
, I’ll get the right result on myselect
?2b. What I understood on the subject (speaking very layily), the
await
ago the task in the background, so in my example if theinsert
take long to be done, myselect
will not perform with the information that thisinsert
would 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?