Skip to content

AddEventHandlerWithRetries with the getHandler parameter does not work #450

@AlexTroshkin

Description

@AlexTroshkin

I have the following subscription and an event handler into which I need to inject a command service that works with a specific shard of KurrentDB

var subscriptionId = $"Ledger.StarterDailyAccountOpener.AccountCluster.{location.ClusterIndex}";

builder.Services
    .AddKeyedSingleton(subscriptionId, (serviceProvider, _) => serviceProvider.GetRequiredKeyedService<EventStoreClient>(shardKey))
    .AddSubscription<StreamSubscription, StreamSubscriptionOptions>(subscriptionId, subscription => subscription
        .Configure(options =>
        {
            options.StreamName = new StreamName("$et-V1.AccountCreated");
            options.ResolveLinkTos = true;
            options.ThrowOnError = true;
        })
        .AddEventHandlerWithRetries(serviceProvider => ActivatorUtilities.CreateInstance<StarterDailyAccountOpener>(
            serviceProvider,
            serviceProvider.GetRequiredKeyedService<ICommandService<DailyAccountState>>(shardKey)), _defaultResiliencePolicy));

When starting the host, I get an exception that the StarterDailyAccountOpener handler is not registered with the subscriptionId key. The problem is in the AddCompositionEventHandler method, specifically here

Services.TryAddKeyedSingleton(SubscriptionId, getInnerHandler);

In the container, not an instance of the handler is registered, but a function to obtain the handler. It should be

Services.TryAddKeyedSingleton(SubscriptionId, (serviceProvider, _) => getInnerHandler(serviceProvider))

Metadata

Metadata

Assignees

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