title: EF Core DB Context Injection In Custom Authorization Handler
body: <p>I need to use entity framework in my custom authorization handler. But it's not working. It fails on runtime. I am getting this error in response body:</p>
  
  <pre><code>&lt;h2 class="stackerror"&gt;InvalidOperationException: Cannot consume scoped service &amp;#x27;SomeDbContext&amp;#x27; from singleton &amp;#x27;Microsoft.AspNetCore.Authorization.IAuthorizationHandler&amp;#x27;.&lt;/h2&gt;
  </code></pre>
  
  <p>I can't inject DB Context like this. How can I use db context in my custom authorization handler? </p>
  
  <p>In my custom authorization handler class:</p>
  
  <pre><code>public class CustomAuthorizationHandler : AuthorizationHandler&lt;CustomAuthRequirement&gt;
  {
      private readonly SomeDbContext _dbContext;
  
      public CustomAuthorizationHandler(SomeDbContext context)
      {
          _dbContext = context;
      }
  
      protected override Task HandleRequirementAsync(AuthorizationHandlerContext context, CustomAuthRequirement requirement)
      {
          ...
  
          //Some datatable read operations with _dbContext
  
          ...
      }
  }
  </code></pre>
  
  <p>In my Startup.cs:</p>
  
  <pre><code>    public void ConfigureServices(IServiceCollection services)
      {
          services.AddDbContext&lt;SomeDbContext&gt;(options =&gt;
                options.UseNpgsql(Configuration.GetConnectionString("DefaultConnection")));
  
          services.AddSingleton&lt;IAuthorizationPolicyProvider, CustomAuthPolicyProvider&gt;();
  
          services.AddSingleton&lt;IAuthorizationHandler, CustomAuthorizationHandler&gt;();
  
          ...
      }
  </code></pre>