使用c#进行异步调用只适用于逐步调试

时间:2016-10-06 21:03:41

标签: c# asp.net entity-framework asynchronous

因此,我目前正在使用.NET构建Web Api,并使用实体框架进行异步调用。

在我的控制器的PUT端点上,我试图了解用户是否已经属于另一个级别,或者他是否完全在数据库中,这是控制器代码:

    [HttpPut]
    public async Task<IHttpActionResult> PutCommittee(CommitteeViewModel committee)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (!User.IsInRole("Dean") && !User.IsInRole("Chair"))
            return Unauthorized();


        var user = await db.Users.Where(u => u.Cn == User.Identity.Name).FirstOrDefaultAsync();
        if (user == null) { return BadRequest("Your user does not exist"); }
        if (User.IsInRole("Dean"))
        {
            var college = await db.Colleges.Where(c => c.Dean.Cn == user.Cn).FirstOrDefaultAsync();
            if (college == null) { return BadRequest("You're not a Dean of any college"); }
            committee.Name = _utils.getCollegeCommitteeName(college);

        }
        else
        {
            var department = await db.Departments.Where(d => d.Chair.Cn == user.Cn).FirstOrDefaultAsync();
            if (department == null) { return BadRequest("You're not a Chair of any college"); }
            committee.Name = _utils.getDepartmentCommitteeName(department);
        }

        var model = await db.Commitees.Where(c => c.Name == committee.Name).FirstOrDefaultAsync();
        if (model == null)
            return BadRequest("You have no committee");

        var tuple = await getUsers(committee);
        model.Users = tuple.Item1;
        if (model.Users == null)
            return BadRequest(tuple.Item2);

        db.Entry(model).State = EntityState.Modified;

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            throw;
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

以下是检查用户的功能:

        private async Task<Tuple<List<User>, string>> getUsers(CommitteeViewModel committee)
    {
        string error = "";
        List<User> users = new List<User>();
        var tuple = new Tuple<List<User>, string>(users, error);
        var role = await db.Roles.Where(r => r.Name == "Committee").FirstOrDefaultAsync();
        foreach (UserViewModel u in committee.Users)
        {
            var user = await db.Users.Where(us => us.Cn == u.Cn).FirstOrDefaultAsync();


            if (user != null)
            {
                if (user.Role.Name == "Chair" || user.Role.Name == "Dean")
                {
                    error = "User " +  user.Name + " is a " + user.Role.Name + " and cannot be member of a review committee";
                    return tuple;
                }

                users.Add(user);
            }
            else
            {
                user = _loginProvider.generateUser(u.Cn, role);
                db.Users.Add(user);
                await db.SaveChangesAsync();
                users.Add(user);
            }
        }



        return tuple;
    }

我正在使用元组,因为异步方法不支持OUT参数,以防出现错误。

所以问题是,当我删除我的前端用户(然后发送带有更新数组的put请求),然后我逐步调试,它会删除它,但是当我不这样做时,如果我在try块中放置一个断点,变量model.Users包含前一个数组(模型中的原始数据),这只发生在我从一个数组中删除一个用户时,这很奇怪事情是,当我编写代码同步

时也会发生这种情况

感谢您的帮助。

1 个答案:

答案 0 :(得分:-1)

请记住...异步方法运行服务器,所以如果你需要先运行需要先运行,你需要替换一个简单的异步方法。尝试一下,我相信你可以解决它。