Phase 17 of 30 · Topic 17.3

C# 12/13 Experimental Interceptors ([InterceptsLocation])

1Concept

Interceptors allow source generators to substitute calls to specific methods in source code with generated high-performance alternative implementations at compile time.

2Architecture Diagram

app.MapGet("/users", () => "OK");
       │
   Compiler Intercepts Location: (Program.cs, Line 14, Col 5)
       │
   Replaced with direct inlined route execution delegate (Zero Dynamic Routing Overhead!)

3Code Example

C# 13 & .NET 9
using System;

public class InterceptorsConceptDemo
{
    public static void Main()
    {
        Console.WriteLine("C# Interceptors enable zero-overhead Minimal API endpoint mapping and Dapper query inlining.");
    }
}

4Expected Output

C# Interceptors enable zero-overhead Minimal API endpoint mapping and Dapper query inlining.

5Key Takeaways

  • Interceptors power ASP.NET Core Request Delegate Generator (RDG).
  • Enables compile-time query validation for Dapper and Entity Framework.
  • Requires `<Features>InterceptorsPreview</Features>` in project files.