Guillermo Contreras
Guillermo Contreras

Reputation: 61

Mediatr Handler was not found for request of type

I come to you because I have the following problem and I need your help.

I have 2 projects as you can see in the image.

enter image description here

The _admin project is the main project and the _config project once compiled remains as a plugin of the _admin project (This loads all the dependencies of the config project).

In both cases I am using Mediatr and they work perfectly when I run the applications separately.

But when I compile the _config project to be run as part of the _admin project I get the following error.

Handler was not found for request of type MediatR.IRequestHandler`2[...Plugins.Configurations.Application.ConfigurationValues.Querys.GetConfigurationValuesQuery,System.Collections.Generic.IEnumerable`1[...Plugins.Configurations.Application.ConfigurationValues.Dtos.ConfigurationValueDto]]. Register your handlers with the container. See the samples in GitHub for examples.

The code you used is the following (I also tried using the class GetConfigurationValuesQueryHandler):

Plugins.Configurations.Application.DependencyInjection.cs

public static class DependencyInjection
{
    public static IServiceCollection AddConfigurationApplication(this IServiceCollection services)
    {

        services.AddAutoMapper(Assembly.GetExecutingAssembly());
        services.AddValidatorsFromAssembly(Assembly.GetExecutingAssembly());
        services.AddMediatR(Assembly.GetExecutingAssembly());
        //services.AddMediatR(typeof(GetConfigurationValuesQueryHandler).Assembly);
        return services;
    }
}

Plugins.Configurations.Mvc.Startup.cs

public void ConfigureServices(IServiceCollection services)
    {
        services.AddBaseApplication();
        services.AddConfigurationApplication();//<-- Here
        services.AddBaseInfrastructure();
        services.AddConfigurationInfrastructure(Configuration);
        services.AddHttpContextAccessor();
        services.AddHealthChecks().AddDbContextCheck<ConfigurationDbContext>();
        services.AddControllersWithViews();
    }

This error only occurs when the _config project acts as an _admin plugin. When I run the _config project individually it works perfectly.

  1. Could it be that _config being an _admin plugin, the Startup.cs class of the _config project is not executed? If it were this, how could I solve it so that it executes the AddConfigurationApplication method in a dynamic way.
  2. If not point 1. How could I solve this? without having the need to add the _config project as a direct reference in the _admin project.

In advance I thank you for the help you can give me.

Upvotes: 1

Views: 4080

Answers (2)

crgolden
crgolden

Reputation: 4614

The AddMediatr extension method takes an array of assemblies, so you may want to utilize that:

var assembly1 = Assembly.GetExecutingAssembly();
var assembly2 = typeof(GetConfigurationValuesQueryHandler).Assembly;
services.AddMediatR(assembly1, assembly2);

That way it will add all your handlers from both projects.

Upvotes: 0

Adem Catamak
Adem Catamak

Reputation: 2009

services.AddMediatR(Assembly.GetExecutingAssembly());

The above code snippet is not a pure method. This method will behave differently depending on the change of the startup project.

When you choose the Admin project as the startup project, the AddConfigurationApplication method will add the Automapper classes, Validation classes and Mediatr classes in the Admin project not Configuration project.

Your problem will be resolved when you provide assembly information about the Configuration project strictly.

public static IServiceCollection AddConfigurationApplication(this IServiceCollection services)
{
    Assembly configurationAppAssembly = typeof(CLASS_FROM_CONFIGURATION_PROJECT).Assembly;
    ...    
    serviceCollection.AddMediatR(configurationAppAssembly);
}

Upvotes: 1

Related Questions