title: await on UserManager causes the error A second operation started on this context before a previous operation completed
body: <p><strong>BACKGROUND</strong></p>
  
  <p>I am working on a <strong>.Net Core API</strong> to drive a lyrics application. Users can sign up, submit artists &amp; lyrics, and earn kudos / XP points in the process. Basically a community-driven lyrics website.</p>
  
  <p><strong>CODE</strong></p>
  
  <p>Here's my ArtistController class:</p>
  
  <pre><code>[Route("api/artists")]
  public class ArtistsController : Controller
  {
    private readonly IPermissionsService _permissionsService;
    private readonly IArtistsService _artistsService;
  
    public ArtistsController(IArtistsService artistsService, IPermissionsService permissionsService)
    {
      _permissionsService = permissionsService ?? throw new ArgumentNullException(nameof(permissionsService));
      _artistsService = artistsService ?? throw new ArgumentNullException(nameof(artistsService));
    }
  
    [HttpGet("{slug}")]
    [HttpGet("{slug}/lyrics", Name = "GetArtist")]
    public async Task&lt;IActionResult&gt; GetArtist(string slug)
    {
      if (!_artistsService.ArtistExists(slug)) return NotFound();
      var permissions = await _permissionsService.GetPermissions(HttpContext);
      var artist = _artistsService.GetArtistBySlug(slug, permissions.UserId, permissions.IsAdministrator);
      if (artist == null) return NotFound();
      return Ok(artist);
    }
  
    // other methods omitted
  }
  </code></pre>
  
  <p>In the spirit of testability, I created an <code>IPermissionsService</code>, this way when I come to Unit Test the controller, I can easily do so without worrying about <code>HttpContext</code> and <code>User</code>.</p>
  
  <p>Here is the code for <code>PermissionsService</code> class:</p>
  
  <pre><code>public class PermissionsService : IPermissionsService
  {
    private string _userId;
    private bool _isAdministrator;
    private HttpContext _httpContext;
    private readonly UserManager&lt;BbUser&gt; _userManager;
  
    public PermissionsService(UserManager&lt;BbUser&gt; userManager)
    {
      _userManager = userManager;
    }
  
    public async Task&lt;Permissions&gt; GetPermissions(HttpContext httpContext)
    {
      _httpContext = httpContext;
      PopulateUserIdAndIsAdminFlag();
      var permissions = new Permissions
      {
        UserId = _userId,
        IsAdministrator = _isAdministrator
      };
  
      return await Task.Run(() =&gt; permissions);
    }
  
    private async void PopulateUserIdAndIsAdminFlag()
    {
      if (!IsAuthenticated()) return;
      var username = _httpContext.User.FindFirstValue(ClaimTypes.NameIdentifier);
      var user = await _userManager.FindByNameAsync(username);
      var roles = await _userManager.GetRolesAsync(user);
      _userId = user.Id;
      _isAdministrator = roles.Contains("Admin");
    }
  
    private bool IsAuthenticated()
    {
      return _httpContext.User.Identity.IsAuthenticated;
    }
  }
  </code></pre>
  
  <p><strong>PROBLEM</strong></p>
  
  <p>When I run the API and attempt to call that end-point. I get the following error:</p>
  
  <blockquote>
    <p>A second operation started on this context before a previous operation completed. Any instance members are not guaranteed to be thread safe.</p>
  </blockquote>
  
  <p>The message is pretty clear, but I am not sure how to overcome this error. Before moving that logic out to the <code>PermissionsService</code> I was not getting the error and all was working fine!</p>