Skip to content

[Feature Request]: Add support for resolving keyed services from dependency injection container #646

@dorlugasigal

Description

@dorlugasigal

Is your request related to a problem you have?

When using .NET's keyed services, KafkaFlow's resolver doesn't provide a way to resolve services registered with keys from the dependency injection container.

Currently, we have to use a workaround by creating a temporary service provider to pre-resolve keyed services before configuring KafkaFlow, or to pass the key as a field to the implementing class. which adds unnecessary complexity and potential performance overhead to our applications:

// Create a temporary service provider to resolve keyed services
var tempServices = new ServiceCollection();
foreach (var descriptor in services) {
    tempServices.Add(descriptor);
}
using var tempProvider = tempServices.BuildServiceProvider();
var processor = tempProvider.GetKeyedService<IMessageProcessor<TInput, TOutput>>(processorKey);

Describe the solution you'd like

We'd like KafkaFlow's resolver to support resolving keyed services directly. The IDependencyResolver interface could be extended to include methods like ResolveKeyedService<T>(object key) and that would delegate to the underlying .NET dependency injection container.

For example, when configuring middleware, we'd like to be able to do something like:

middlewares.Add<GenericProcessingMiddleware<TInput, TOutput>>(resolver => 
{
    // Resolver should support getting keyed services
    return new GenericProcessingMiddleware<TInput, TOutput>(
        resolver.GetKeyedService<IMessageProcessor<TInput, TOutput>>(processorKey),
        resolver.GetKeyedService<IMessageValidator<TInput>>(processorKey),
        resolver.GetRequiredService<IProducerAccessor>(),
        // Other dependencies...
    );
});

This would align KafkaFlow's DI capabilities with the latest .NET features and enable more flexible service resolution patterns.

Are you able to help bring it to life and contribute with a Pull Request?

No

Additional context

The current workaround of creating a temporary service provider works but feels like a hack and could lead to potential issues with scoped services or memory leaks if not managed carefully.

I would love to implement it myself by im not familiar with different types of di like Unity and such

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions