I want to return a Func middle of a using block. Should I worry about disposing of, before the user runs the result Func?
A sample code:
private IDbContextTransaction _transaction;
public Func<Task> BeginTransaction()
{
Task lockDispose = CommitTransaction();
using (_transaction = _dbContext.Database.BeginTransaction())
{
return async() =>
{
await lockDispose;
};
Task.WaitAll(lockDispose); //This code is unreachable.
}
}
private async Task CommitTransaction()
{
_transaction.Commit();
await Task.CompletedTask;
}
Note that the execution time of the result Func is up to the user of this service.
I checked This Question and it's not my answer.