Skip to main content

Source Generators

Dispatch includes Roslyn source generators that enable ahead-of-time (AOT) compilation and Native AOT support. Source generators analyze your code at compile time and produce explicit registrations, eliminating the need for runtime reflection.

Before You Start

  • .NET 8.0+ (or .NET 9/10 for latest features)
  • Install the required packages:
    dotnet add package Excalibur.Dispatch
    dotnet add package Excalibur.Dispatch.SourceGenerators # source generators
  • Familiarity with handlers and dependency injection

Key Features

  • Compile-time service discovery - No runtime assembly scanning
  • Full AOT support - Compatible with .NET Native AOT publishing
  • Trimming safe - Works with IL trimming without losing types
  • Opt-in design - Only marked types are auto-registered

Quick Start

using Excalibur.Dispatch.Abstractions;
using Microsoft.Extensions.DependencyInjection;

// Mark services for auto-registration
[AutoRegister]
public class OrderHandler : IDispatchHandler<CreateOrderCommand>
{
// Implementation...
}

// In Program.cs
builder.Services.AddGeneratedServices();

Documentation

TopicDescription
Getting StartedQuick start guide with examples
ArchitectureDeep dive into all generators

Available Generators

GeneratorStatusPurpose
ServiceRegistrationSourceGeneratorActiveAuto-registers [AutoRegister] types
HandlerRegistrySourceGeneratorActiveHandler discovery at compile time
HandlerInvokerSourceGeneratorActiveZero-reflection handler invocation
JsonSerializationSourceGeneratorActiveAOT-compatible JSON serialization
And 6 more...ActiveSee full list

Installation

dotnet add package Excalibur.Dispatch.SourceGenerators

The [AutoRegister] attribute is provided by Excalibur.Dispatch.Abstractions.

See Also