Skip to content

Instantly share code, notes, and snippets.

@bdaniel7
Last active August 6, 2019 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bdaniel7/ee07f1278500febc160fec7263f0477a to your computer and use it in GitHub Desktop.
Save bdaniel7/ee07f1278500febc160fec7263f0477a to your computer and use it in GitHub Desktop.
Multiple registrations of INotificationHandler
public class MeasureEmailNotification : INotification
{
public int Id { set; get; }
}
public class MeasureEmailNotificationHandler : INotificationHandler<MeasureEmailNotification>
{
public async Task Handle(MeasureEmailNotification emailNotification, CancellationToken cancellationToken)
{
// just to compile
await Console.Out.WriteLineAsync(emailNotification.Id);
}
}
public class RegulationEmailNotification : INotification
{
public int Id { set; get; }
}
public class RegulationEmailNotificationHandler : INotificationHandler<RegulationEmailNotification>
{
public async Task Handle(RegulationEmailNotification emailNotification, CancellationToken cancellationToken)
{
// just to compile
await Console.Out.WriteLineAsync(emailNotification.Id);
}
}
public IServiceProvider ConfigureServices(IServiceCollection services)
{
// domainAssembly contains all IRequest<>, IRequestHandler<>, INotification, INotificationHandler<> implementations
var domainAssembly = typeof(RegulationEmailNotificationHandler).Assembly;
services.AddMediatR(domainAssembly);
// ContainerBuilder is from Autofac
var builder = new ContainerBuilder();
builder.RegisterAssemblyTypes(domainAssembly)
.AsImplementedInterfaces()
.InstancePerLifetimeScope();
builder.Populate(services);
Container = builder.Build();
return new AutofacServiceProvider(Container);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment