title: c# async and await error. Code not processing after await
body: <p>I have started using async and await in c# and i have come along a problem, and get an error saying </p>
  
  <p><code>An asynchronous module or handler completed while an asynchronous operation was still pending.</code></p>
  
  <p>I am using Entity Framework 6 and i have a base service which each service inherits from which looks like so</p>
  
  <pre><code>public BaseService(TEntity) where TEntity : class {
      private DataContext _context;
  
      public BaseService(DataContext context) {
          _context = context;
      }
  
      public virtual async Task&lt;TEntity&gt; AddAsync(TEntity t)
      {
          if (_context.Entry&lt;TEntity&gt;(t).State == EntityState.Detached)
          {
              _context.Set&lt;TEntity&gt;().Add(t);
          }
  
          _context.Entry&lt;TEntity&gt;(t).State = EntityState.Added;
  
          await _context.SaveChangesAsync();
  
          return t;
      }
  }
  </code></pre>
  
  <p>And i inherit from this base for UserService, PermissionService and GroupService</p>
  
  <pre><code>public class UserService : BaseService&lt;User&gt;
  {
      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&lt;Group&gt;
  {
      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&lt;Permission&gt;
  {
      public PermissionService(DataContext context)
          : base(context)
      {
  
      }
  }
  </code></pre>
  
  <p>Now in my data initializer i have my seed method, and i need to call these async methods, so i have added the async property and it looks something like this</p>
  
  <pre><code>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 =&gt; 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 =&gt; e.UserName == "name@mail.com").FirstOrDefault();
          var group = context.Groups.Where(e =&gt; e.Name == "Admin").FirstOrDefault();
  
          await userService.AddToGroup(user, group);
      }
  }
  </code></pre>
  
  <p>Now once my initializer is called, only 1 permission is added to the database, nothing else happens.</p>
  
  <p>Have i setup my async stuff correctly?</p>
  
  <p>My understanding about async/await is that if i call a method like this</p>
  
  <pre><code>await my_async_method
  </code></pre>
  
  <p>Then this method will be called and nothing will process after it until this task has been completed.  In the method i am calling, i should have</p>
  
  <pre><code>public async Task my_async_method() {
      await MyCallAsync();
  }
  </code></pre>
  
  <p>Do i need to have a return parameter of Task?  Should it be void?</p>
  
  <p>Any guidance welcome</p>