Skip to content

ConsumeContextConverterGenerator fails to detect EventHandler.On<T> calls #538

@alexeyzimarev

Description

@alexeyzimarev

Problem

ConsumeContextConverterGenerator fails to generate consume context converters for event types registered via EventHandler.On<T>(), causing runtime warnings:

[ReadModelProjection] Static context conversion not found for message type MyEvent, using reflections.

Root cause

The generator's ShouldTreatGenericOnAsEvent heuristic (line 135-142 of ConsumeContextConverterGenerator.cs) requires the type parameter name to contain "Event":

static bool ShouldTreatGenericOnAsEvent(IMethodSymbol method) {
    if (method is not { Name: "On" }) return false;
    var def = method.OriginalDefinition;
    if (def.TypeParameters.Length != 1) return false;
    var paramName = def.TypeParameters[0].Name;
    return paramName.IndexOf("Event", StringComparison.OrdinalIgnoreCase) >= 0;
}

But EventHandler.On<T> defines the type parameter as just T:

protected void On<T>(HandleTypedEvent<T> handler) where T : class {

Since "T".IndexOf("Event") returns -1, the generator skips all On<T>() calls and emits the marker file with zero conversions.

Expected behavior

The generator should detect On<T> calls based on the method belonging to EventHandler (or matching the delegate signature HandleTypedEvent<T>), not based on the generic parameter name. The concrete type argument (e.g., SessionStartedEvent) is always known at the call site regardless of what the parameter is named.

Workaround

Manual registration in consuming code:

MessageConsumeContextConverter.Register(static context => context.Message switch {
    MyEvent1 => new MessageConsumeContext<MyEvent1>(context),
    MyEvent2 => new MessageConsumeContext<MyEvent2>(context),
    // ... all event types
    _ => null
});

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions