LuckyPennySoftware/MediatR · error · ArgumentException

No assemblies found to scan. Supply at least one assembly to

Error message

No assemblies found to scan. Supply at least one assembly to scan for handlers.

What it means

AddMediatR(MediatRServiceConfiguration) requires at least one assembly to scan for handlers. The guard checks configuration.AssembliesToRegister and throws ArgumentException with a clear message if empty, preventing a silent no-op registration that would later surface as unresolved handlers.

Source

Thrown at src/MediatR/MicrosoftExtensionsDI/MediatRServiceCollectionExtensions.cs:51

        var serviceConfig = new MediatRServiceConfiguration();

        configuration.Invoke(serviceConfig);

        return services.AddMediatR(serviceConfig);
    }
    
    /// <summary>
    /// Registers handlers and mediator types from the specified assemblies
    /// </summary>
    /// <param name="services">Service collection</param>
    /// <param name="configuration">Configuration options</param>
    /// <returns>Service collection</returns>
    public static IServiceCollection AddMediatR(this IServiceCollection services, 
        MediatRServiceConfiguration configuration)
    {
        if (!configuration.AssembliesToRegister.Any())
        {
            throw new ArgumentException("No assemblies found to scan. Supply at least one assembly to scan for handlers.");
        }

        ServiceRegistrar.SetGenericRequestHandlerRegistrationLimitations(configuration);

        ServiceRegistrar.AddMediatRClassesWithTimeout(services, configuration);

        ServiceRegistrar.AddRequiredServices(services, configuration);

        return services;
    }
    
    internal static void CheckLicense(this IServiceProvider serviceProvider)
    {
        if (LicenseChecked)
        {
            return;
        }

View on GitHub (pinned to 916ef1b3d6)

Solutions

  1. Add `cfg.RegisterServicesFromAssemblyContaining<Startup>()` (or FromAssembly(typeof(...).Assembly)) before AddMediatR(cfg).
  2. Prefer the assembly-accepting AddMediatR overloads (e.g. services.AddMediatR(cfg => cfg.RegisterServicesFromAssembly(assembly))) which make the dependency explicit.
  3. Assert configuration.AssembliesToRegister.Count > 0 in a startup test.

Example fix

// before
var cfg = new MediatRServiceConfiguration();
services.AddMediatR(cfg); // ArgumentException
// after
var cfg = new MediatRServiceConfiguration();
cfg.RegisterServicesFromAssemblyContaining<Startup>();
services.AddMediatR(cfg);
Defensive patterns

Strategy: validation

Validate before calling

if (configuration.AssembliesToRegister.Count == 0)
    throw new InvalidOperationException("Register at least one assembly before AddMediatR.");

Type guard

static bool HasAssemblies(MediatRServiceConfiguration c) => c.AssembliesToRegister.Count > 0;

Prevention

When it happens

Trigger: Building a MediatRServiceConfiguration and calling AddMediatR(cfg) without ever calling RegisterServicesFromAssembly*, RegisterServicesFromAssemblies, or RegisterServicesFromAssemblyContaining<T>.

Common situations: Calling cfg.AddMediatR() with the parameterless builder then AddMediatR(cfg); using AddMediatR(cfg) where cfg was constructed manually without assembly registration; ordering mistake where assembly registration is commented out during refactor.

Related errors


AI-assisted analysis of LuckyPennySoftware/MediatR@916ef1b3d6 (2026-08-13). Data as JSON: /api/errors/3311f7aa1aad87c0. Report an issue: GitHub.