C#任务重写功能与自定义任务

时间:2015-06-20 19:58:03

标签: c# async-await task

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

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 Authentication的一部分,其中UserManager是

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

3 个答案:

答案 0 :(得分:2)

你永远不应该使用任务构造函数。只需使用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();
  }
}

答案 1 :(得分:0)

您只是忘了启动新创建的任务:

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;
 }

答案 2 :(得分:0)

您可以使用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();
        });
}