title: ASP NET Core Use Async Mediator with DbContext
body: <p>I am trying to access the dbContext via Dependency Injection. And it works.</p>
  
  <p>I am adding the dbcontext in startup</p>
  
  <pre><code>Services.AddDbContextPool&lt;ApplicationDbContext&gt;(options =&gt;
              {
                  options.UseMySql(Configuration["ConnectionStrings:DefaultConnection"],
                      b =&gt; b.MigrationsAssembly("Persistence"));
                  options.UseOpenIddict();
              });
  </code></pre>
  
  <p>I am Also adding all the request Handlers in Mediator</p>
  
  <pre><code>services.AddMediatR(typeof(ChangePasswordRequestHandler).GetTypeInfo().Assembly);
  </code></pre>
  
  <p>I am calling a request inside in another request in the mediator. Here is a sample</p>
  
  <pre><code>    public class CreateComment : IRequestHandler&lt;CreateCommentRequestModel, ResponseViewModel&gt;
      {
          private readonly ApplicationDbContext _context;
          private readonly IMediator _mediator;
          public CreateComment(ApplicationDbContext context, IMediator mediator)
          {
              _context = context;
              _mediator = mediator;
          }
  
          public async Task&lt;ResponseViewModel&gt; Handle(CreateCommentRequestModel request, CancellationToken cancellationToken)
          {
              _ = _mediator.Send(new SendCommentNotificationRequestModel(), CancellationToken.None);
              return new ResponseViewModel();
          }
  </code></pre>
  
  <p>Here is another request Handler</p>
  
  <pre><code>    public class SendCommentNotification : IRequestHandler&lt;SendCommentNotificationRequestModel, ResponseViewModel&gt;
      {
          private readonly ApplicationDbContext _context;
          private readonly IMediator _mediator;
          public SendCommentNotification(ApplicationDbContext context, IMediator mediator)
          {
              _context = context;
              _mediator = mediator;
          }
  
          public async Task&lt;ResponseViewModel&gt; Handle(SendCommentNotificationRequestModel request, CancellationToken cancellationToken)
          {
              await _mediator.Send(new SendNotificationToManyRequestModel(), CancellationToken.None);
              return new ResponseViewModel();
          }
      }
  </code></pre>
  
  <p>Here is my Third Handler</p>
  
  <pre><code>    public class SendNotificationToMany : IRequestHandler&lt;SendNotificationToManyRequestModel, ResponseViewModel&gt;
      {
          private readonly ApplicationDbContext _context;
          private readonly INotificationService _notificationService;
          public SendNotificationToMany(
              INotificationService notificationService, ApplicationDbContext context)
          {
              _notificationService = notificationService;
              _context = context;
          }
  
          public async Task&lt;ResponseViewModel&gt; Handle(SendNotificationToManyRequestModel request,
              CancellationToken cancellationToken)
          {
             //DbContext is disposed hence not usable when accessed here.
  
              return new ResponseViewModel();
          }
      }
  </code></pre>
  
  <p>The issue is that the DbContext is disposed when working on the third Handler. What can I do to resolve this?</p>
  
  <p>If I add <code>await</code> on the first handler then it works as it should but I don't want to wait for it I want this to run in background.</p>
  
  <p>I know the fix is very small but I am unable to figure it out.</p>