Skip to content

[Feature]: Configurable Handler Lifetime #216

@eneshoxha

Description

@eneshoxha

Title: Allow configuring handler service lifetime (Scoped, Transient, Singleton)

Labels: enhancement, mediator

Body:

Problem

In ServiceCollectionExtensions.cs, all handlers (ICommandHandler, IQueryHandler, INotificationHandler, IStreamQueryHandler) are registered with WithScopedLifetime() hardcoded. There is no way to configure this.

Some architectures prefer:

  • Transient handlers when handlers are stateless and scope leaks are a concern
  • Singleton handlers for high-throughput scenarios with stateless handlers

Proposed Solution

Add a HandlerLifetime property to MediatorOptions:

public class MediatorOptions
{
    public ServiceLifetime HandlerLifetime { get; set; } = ServiceLifetime.Scoped;
    // ...existing properties...
}

Use this in RegisterHandlers instead of hardcoded .WithScopedLifetime().

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfeatureThis label is in use for minor version increments
    No fields configured for Feature.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions