Skip to content

Add IDbCommandInterceptor or similar to allow injecting custom query execution service #15066

Description

@kosinsky

Sometime application needs more control on executing build SQL statement in the database to add custom retry logic, circuit breakers, CPU usage logging and throttling, injecting session-wide information to support Row-Level-Security etc. That requires injecting custom code that will get SQL Command from the EF Core (or just text + list of parameters), executing it and returning IDataReader back to the EF Core.

EF6 supported that approach by deriving from DbCommandInterceptor and overriding ReaderExecuting and ScalarExecuting.

This issue is a detailed case for #626

Activity

  1. self-assigned this
    on Mar 20, 2019
  2. added this to the 3.0.0 milestone on Mar 22, 2019
  3. tstone84 commented on Mar 26, 2019

    @tstone84

    "injecting session-wide information to support Row-Level-Security" is a critical feature preventing us from moving to EF Core. Currently, in EF6 we use DbCommandInterceptor and DbConnectionInterceptor to inject SQL to handle setting session context for SQL Server Row Level Security. Is there an existing way to do this in EF Core?

  4. changed the title [-]Support injecting custom query execution service[/-] [+]Implement DbCommandInterceptor or similar to allow injecting custom query execution service[/+] on Jun 15, 2019
  5. changed the title [-]Implement DbCommandInterceptor or similar to allow injecting custom query execution service[/-] [+]Add IDbCommandInterceptor or similar to allow injecting custom query execution service[/+] on Jun 15, 2019
  6. added a commit that references this issue on Jun 15, 2019
    fccc1e3
  7. ajcvickers commented on Jun 15, 2019

    @ajcvickers
    Contributor

    @kosinsky PR: #16113 Does this look like it covers what you need?

  8. added a commit that references this issue on Jun 16, 2019
    f5e663c
  9. kosinsky commented on Jun 16, 2019

    @kosinsky
    ContributorAuthor

    @kosinsky PR: #16113 Does this look like it covers what you need?

    @ajcvickers Partially. Looks like when implementing IDbCommandInterceptor we don't have a way to access original DbContext or service provider (and then get DbContext scoped service that we could use for command execution). Am I missing something?
    In my use case, we attaching our custom SqlResourceComponent (it's responsible for query execution, RLS, circuit breakers etc.) to DbContext and then in IDbCommandIntercepor we have something like:

    public override void ReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
            {
                var context = interceptionContext.DbContexts.FirstOrDefault() as IComponentContext;
                if (context != null)
                {
                    if (context.Component != null)
                    {
                        var result = context.Component.ExecuteContextCommand(command as SqlCommand);
                        interceptionContext.Result = result;
                        return;
                    }
                        base.ReaderExecuting(command, interceptionContext);
                        return;
                }
            }
    
  10. nphmuller commented on Jun 17, 2019

    @nphmuller

    @kosinsky @ajcvickers Injecting the DbContext in the interceptor's constructor would probably work, right? Or are only Singleton interceptors supported?

  11. added 2 commits that reference this issue on Jun 17, 2019
    4312a94
    c89f015
  12. added a commit that references this issue on Jun 18, 2019
    7839c87
  13. ajcvickers commented on Jun 19, 2019

    @ajcvickers
    Contributor
  14. nphmuller commented on Jun 19, 2019

    @nphmuller

    @ajcvickers Great! Just curious, but is there any reason the DbContext couldn't by injected in the interceptor via its constructor? Or would that give a circular reference somehow? Or is there perhaps another benefit I'm missing?

  15. ajcvickers commented on Jun 19, 2019

    @ajcvickers
    Contributor

    @nphmuller Interceptors are currently resolved only from the internal service provider. So having one depend on DbContext will usually not work, since it's not usually registered in the internal service provider. The interceptor might be able to depend on ICurrentDbContext to achieve the same thing, but that wouldn't help when an app is using an interceptor that is not registered in D.I., which is what I expect will be the most common usage.

  16. nphmuller commented on Jun 19, 2019

    @nphmuller

    @ajcvickers I didn't think about the non DI scenario, since DI seems so much like the normal route in ASP.NET Core. Thanks for the clear explanation!

    I learned about ICurrentDbContext when I looked at these PRs and it seems like an easy way to access the current DbContext in extension points.

  17. modified the milestones: 3.0.0, on Jul 2, 2019
  18. modified the milestones: , 3.0.0 on Nov 11, 2019
  19. removed their assignment
    on Sep 1, 2024
  20. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions