Skip to content

Instantly share code, notes, and snippets.

@anderly
Last active June 14, 2022 08:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anderly/53e3b98c440479a9171643a79d2720d7 to your computer and use it in GitHub Desktop.
Save anderly/53e3b98c440479a9171643a79d2720d7 to your computer and use it in GitHub Desktop.
MediatR Pipeline Behavior Registration.
// Register MediatR Pipeline Behaviors
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(LoggingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(ValidationBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(CachingBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(FallbackBehavior<,>));
services.AddTransient(typeof(IPipelineBehavior<,>), typeof(RetryBehavior<,>));
// ICachePolicy discovery and registration
services.Scan(scan => scan
.FromAssemblies(assembly)
.AddClasses(classes => classes.AssignableTo(typeof(ICachePolicy<,>)))
.AsImplementedInterfaces()
.WithTransientLifetime());
// IFallbackHandler discovery and registration
services.Scan(scan => scan
.FromAssemblies(typeof(Startup).Assembly)
.AddClasses(classes => classes.AssignableTo(typeof(IFallbackHandler<,>)))
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
.AsImplementedInterfaces()
.WithTransientLifetime());
// IFallbackHandler discovery and registration
services.Scan(scan => scan
.FromAssemblies(typeof(Startup).Assembly)
.AddClasses(classes => classes.AssignableTo(typeof(IRetryableRequest<,>)))
.UsingRegistrationStrategy(RegistrationStrategy.Skip)
.AsImplementedInterfaces()
.WithTransientLifetime());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment