繁体   English   中英

C#异步并等待错误。 等待后代码未处理

[英]c# async and await error. Code not processing after await

我已经开始在C#中使用异步并等待,并且遇到了问题,并收到一条错误消息:

An asynchronous module or handler completed while an asynchronous operation was still pending.

我正在使用Entity Framework 6,并且有一个基础服务,每个服务都可以从中继承

public BaseService(TEntity) where TEntity : class {
    private DataContext _context;

    public BaseService(DataContext context) {
        _context = context;
    }

    public virtual async Task<TEntity> AddAsync(TEntity t)
    {
        if (_context.Entry<TEntity>(t).State == EntityState.Detached)
        {
            _context.Set<TEntity>().Add(t);
        }

        _context.Entry<TEntity>(t).State = EntityState.Added;

        await _context.SaveChangesAsync();

        return t;
    }
}

我从这个基础上继承了UserService,PermissionService和GroupService

public class UserService : BaseService<User>
{
    public UserService(DataContext context)
        : base(context)
    {

    }

    public async Task AddToGroup(User user, Group group)
    {
        var userGroup = new UserGroup()
        {
            UserId = user.Id,
            User = user,
            GroupId = group.Id,
            Group = group
        };

        user.Groups.Add(userGroup);

        await _context.SaveChangesAsync();
    }
}

public class GroupService : BaseService<Group>
{
    public GroupService(DataContext context)
        : base(context)
    {

    }

    public async Task AddPermissionAsync(Group group, Permission permission)
    {
        var groupPermission = new GroupPermission()
        {
            GroupId = group.Id,
            Group = group,
            PermissionId = permission.Id,
            Permission = permission
        };

        group.Permissions.Add(groupPermission);

        await _context.SaveChangesAsync();
    }

}

public class PermissionService : BaseService<Permission>
{
    public PermissionService(DataContext context)
        : base(context)
    {

    }
}

现在在我的数据初始化程序中,我有我的seed方法,并且我需要调用这些异步方法,因此我添加了async属性,它看起来像这样

protected async override void Seed(DataContext context) {
    await AddPermissions(context);
    await AddGroups(context);
    await AddUsers(context);
    await AddPermissionsToGroups(context);
    await AddUsersToGroups(context);

    // ... more here
}

private async Task AddGroups(DataContext context)
    {
        var groupService = new GroupService(context);

        await groupService.AddAsync(new Group() { Name = "Admin" });
    }

    private async Task AddPermissions(DataContext context)
    {
        var permissionService = new PermissionService(context);

        await permissionService.AddAsync(new Permission("CanAdd", ""));
        await permissionService.AddAsync(new Permission("CanEdit", ""));
        await permissionService.AddAsync(new Permission("CanDelete", ""));
        await permissionService.AddAsync(new Permission("CanView", ""));

        ... more there about 10
    }

    private async Task AddUsers(DataContext context)
    {
        var userService = new UserService(context);

        await userService.AddAsync(new User() { UserName = "name@email.com" });
    }

    private async Task AddPermissionsToGroups(DataContext context)
    {
        var groupService = new GroupService(context);

        // add all permissions to admin
        var adminGroup = context.Groups.Where(e => e.Name == "Admin").FirstOrDefault();
        var permissions = context.Permissions.ToList();

        foreach (var item in permissions)
        {
            await groupService.AddPermissionAsync(adminGroup, item);
        }

    }

    private async Task AddUsersToGroups(DataContext context)
    {
        var userService = new UserService(context);
        var user = context.Users.Where(e => e.UserName == "name@mail.com").FirstOrDefault();
        var group = context.Groups.Where(e => e.Name == "Admin").FirstOrDefault();

        await userService.AddToGroup(user, group);
    }
}

现在,一旦调用了我的初始值设定项,就仅将1个权限添加到数据库中,而没有任何其他反应。

我是否正确设置了异步内容?

我对异步/等待的理解是,如果我调用这样的方法

await my_async_method

然后,将调用此方法,并且在完成此任务之前,将不进行任何处理。 在我打电话的方法中,我应该

public async Task my_async_method() {
    await MyCallAsync();
}

我是否需要Task的返回参数? 它应该是空的吗?

欢迎任何指导

如果我错了,请纠正我,但我认为DbContext每个DbContext仅允许一个异步操作。 因此,您必须将代码更改为:

public async Task AddPermissionAsync(Group group, Permission permission)
{
  using(var context = new DbContext())
  {
    // do the add and save stuff
  }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM