如何在 C# 中等待多个可能未初始化的任务?
How to await multiple possibly uninitialized Tasks in C#?
我有一个 API POST 端点创建资源,该资源可能有多个关系。为确保首先使用有效关系创建资源,我需要检查给定的 ID 是否存在。有多个这样的关系,我不想依次等待每个。这是我的代码:
[HttpPost]
public async Task<ActionResult<Person>> PostPerson(Person person)
{
ValueTask<Person> master, apprentice;
ValueTask<Planet> planet;
ValueTask<Models.LifeFormType> lifeFormType;
if (person.MasterId.HasValue)
{
master = _context.People.FindAsync(person.MasterId);
}
if (person.ApprenticeId.HasValue)
{
apprentice = _context.People.FindAsync(person.ApprenticeId);
}
if (person.FromPlanetId.HasValue)
{
planet = _context.Planets.FindAsync(person.FromPlanetId);
}
if (person.LFTypeId.HasValue)
{
lifeFormType = _context.LifeFormTypes.FindAsync(person.LFTypeId);
}
List<ValueTask> tasks = new List<ValueTask> {master, apprentice, planet, lifeFormType};
// if the above worked I'd process the tasks as they completed and throw errors
// if the given id was not found and such
_context.Attach(person);
// _context.People.Add(person);
await _context.SaveChangesAsync();
return CreatedAtAction("GetPerson", new { id = person.Id }, person);
}
如图所示 here 我想等待 [master,apprentice,planet,lifeFormType]
的列表完成,但在创建 Local variable 'master' might not be initialized before accessing
列表的过程中出现错误。所以我尝试在每次检查资源是否具有该值来创建一个 else
块并以某种方式添加一个 ValueTask.CompletedTask
像这样:
if (person.MasterId.HasValue)
{
master = _context.People.FindAsync(person.MasterId);
}
else
{
master = ValueTask.CompletedTask;
}
但是我收到一条错误消息说 Cannot convert source type 'System.Threading.Tasks.ValueTask' to target type 'System.Threading.Tasks.ValueTask<Models.Person>'
。
如何做到这一点?我想我现在就等待每一个请求。
您可以通过在声明站点初始化 master
来避免这种情况。
最简单的方法是使用 default
关键字。
ValueTask<Person> master = default;
我有一个 API POST 端点创建资源,该资源可能有多个关系。为确保首先使用有效关系创建资源,我需要检查给定的 ID 是否存在。有多个这样的关系,我不想依次等待每个。这是我的代码:
[HttpPost]
public async Task<ActionResult<Person>> PostPerson(Person person)
{
ValueTask<Person> master, apprentice;
ValueTask<Planet> planet;
ValueTask<Models.LifeFormType> lifeFormType;
if (person.MasterId.HasValue)
{
master = _context.People.FindAsync(person.MasterId);
}
if (person.ApprenticeId.HasValue)
{
apprentice = _context.People.FindAsync(person.ApprenticeId);
}
if (person.FromPlanetId.HasValue)
{
planet = _context.Planets.FindAsync(person.FromPlanetId);
}
if (person.LFTypeId.HasValue)
{
lifeFormType = _context.LifeFormTypes.FindAsync(person.LFTypeId);
}
List<ValueTask> tasks = new List<ValueTask> {master, apprentice, planet, lifeFormType};
// if the above worked I'd process the tasks as they completed and throw errors
// if the given id was not found and such
_context.Attach(person);
// _context.People.Add(person);
await _context.SaveChangesAsync();
return CreatedAtAction("GetPerson", new { id = person.Id }, person);
}
如图所示 here 我想等待 [master,apprentice,planet,lifeFormType]
的列表完成,但在创建 Local variable 'master' might not be initialized before accessing
列表的过程中出现错误。所以我尝试在每次检查资源是否具有该值来创建一个 else
块并以某种方式添加一个 ValueTask.CompletedTask
像这样:
if (person.MasterId.HasValue)
{
master = _context.People.FindAsync(person.MasterId);
}
else
{
master = ValueTask.CompletedTask;
}
但是我收到一条错误消息说 Cannot convert source type 'System.Threading.Tasks.ValueTask' to target type 'System.Threading.Tasks.ValueTask<Models.Person>'
。
如何做到这一点?我想我现在就等待每一个请求。
您可以通过在声明站点初始化 master
来避免这种情况。
最简单的方法是使用 default
关键字。
ValueTask<Person> master = default;