异步等待 - 具有自定义任务的 C# 任务重写函数

本文关键字:任务 重写 函数 自定义 等待 异步 | 更新日期: 2023-09-27 17:57:24

我有一个声明如下的函数:

public Task<PossibleUser> FindByNameAsync(string userName)
{
    Task<PossibleUser> task = context.TabelaStudenti.Where(
        apu => apu.NrMatricol == userName)
        .FirstOrDefaultAsync();
    return task;
}

问题是我现在想从该函数返回其他内容,例如,我尝试了:

 public Task<PossibleUser> FindByNameAsync(string userName)
    {
        Student studentUser = context.TabelaStudenti.Where(st => st.NrMatricol == userName).FirstOrDefault();
        Task<PossibleUser> someTask= new Task<PossibleUser>(() =>
        {
            if (studentUser == null)
            {
                return new PossibleUser()
                {
                    Password = studentUser.Parola,
                    UserName = studentUser.UserName
                };
            }
            else
            {
                //just return something else
                return new PossibleUser();
            }
        });
    return someTask;
    }

但它似乎不起作用,我没有得到该函数的响应。如何修改第一个函数以返回我自己的自定义任务?

我必须指定这个函数已经像这样调用:

PossibleUser user = await userManager.FindAsync(context.UserName, context.Password); 

它是 ASP.NET Web API Owin 身份验证的一部分,其中用户管理器是

    UserManagerFactory = () => new UserManager<PossibleUser>(new StudentStore()) { PasswordHasher = new MyPasswordHasher() };

异步等待 - 具有自定义任务的 C# 任务重写函数

永远不要使用任务构造函数。只需使用 async/await

public async Task<PossibleUser> FindByNameAsync(string userName)
{
  Student studentUser = await context.TabelaStudenti.FirstOrDefaultAsync(st => st.NrMatricol == userName);
  if (studentUser != null)
  {
    return new PossibleUser()
    {
      Password = studentUser.Parola,
      UserName = studentUser.UserName
    };
  }
  else
  {
    //just return something else
    return new PossibleUser();
  }
}

你只是忘记启动你新创建的任务:

public Task<PossibleUser> FindByNameAsync(string userName)
{
    Student studentUser = context.TabelaStudenti.Where(st => st.NrMatricol == userName).FirstOrDefault();
    Task<PossibleUser> someTask= new Task<PossibleUser>(() =>
    {
        if (studentUser == null)
        {
            return new PossibleUser()
            {
                Password = studentUser.Parola,
                UserName = studentUser.UserName
            };
        }
        else
        {
            //just return something else
            return new PossibleUser();
        }
    });
    // start your task
    someTask.Start();
    return someTask;
}

或者由于 C# 编译器足够智能,您可以直接从代码中返回PossibleUser

 public async Task<PossibleUser> FindByNameAsync(string userName)
 {
    Task<PossibleUser> task = context.TabelaStudenti.Where(apu => apu.NrMatricol == userName)
    .FirstOrDefaultAsync();
    // get result from async method
    var studentUser = await task;
    if (studentUser == null)
    {
        // Direct return without creating Task
        return new PossibleUser()
        {
            Password = studentUser.Parola,
            UserName = studentUser.UserName
        };
    }
    else
    {
        //just return something else
        return new PossibleUser();
    }
    return someTask;
 }

您可以使用 ContinueWith 如下内容:

public Task<PossibleUser> FindByNameAsync(string userName)
{
    return context.TabelaStudenti
        .FirstOrDefaultAsync(st => st.NrMatricol == userName)
        .ContinueWith(task =>
        {
            var studentUser = task.Result;
            if (studentUser != null)
            {
                return new PossibleUser
                {
                    Password = studentUser.Parola,
                    UserName = studentUser.UserName
                };
            }
            //just return something else
            return new PossibleUser();
        });
}