Mediatr: Manually register Mediatr and handlers

Created on 17 Feb 2017  路  4Comments  路  Source: jbogard/MediatR

I'm using ASP.NET Core, with the built-in container. Automatic registration is done like so:

services.AddMvc();
services.AddMediatR(typeof(Startup));

This automatically 1) configures MediatR, and 2) registers all handlers found in the assembly.

But how do I register my handlers manually?

I saw this in the source:

public static void AddMediatR(this IServiceCollection services, IEnumerable<Type> handlerAssemblyMarkerTypes)
{
    AddRequiredServices(services);
    AddMediatRClasses(services, handlerAssemblyMarkerTypes.Select(t => t.GetTypeInfo().Assembly));
}

I assume I should call AddRequiredServices() to setup MediatR itself, and then manually register my handlers using services.AddTransient<MyHandler>().

But AddRequiredServices() is private.

Most helpful comment

Yeah. Would be useful if that method were public though.

All 4 comments

Register handlers manually?

Don't use this package? The point of this package is to automatically register handlers.

@jbogard My point is if I manually register handlers, surely there is more I need to do to configure MediatR itself?

The AddMediatR() automatic registration calls

AddRequiredServices(services);
AddMediatRClasses(services, handlerAssemblyMarkerTypes.Select(t => t.GetTypeInfo().Assembly));

The second is to register handlers, which I'd like to do manually.

But the first first is for MediatR itself. Which is private. I assume I need to implement that?

Oh yeah, at that point I'd just look at the code inside this and copy-paste what you need for your project.

Yeah. Would be useful if that method were public though.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

nevaenuf picture nevaenuf  路  4Comments

etechnologiesng picture etechnologiesng  路  3Comments

sq735 picture sq735  路  3Comments

TDK1964 picture TDK1964  路  5Comments

jakerabjohns15 picture jakerabjohns15  路  5Comments