FluentValidationForMassTransit 3.0.0

.NET 6.0 .NET Standard 2.0
dotnet add package FluentValidationForMassTransit --version 3.0.0
NuGet\Install-Package FluentValidationForMassTransit -Version 3.0.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="FluentValidationForMassTransit" Version="3.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FluentValidationForMassTransit --version 3.0.0
#r "nuget: FluentValidationForMassTransit, 3.0.0"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install FluentValidationForMassTransit as a Cake Addin
#addin nuget:?package=FluentValidationForMassTransit&version=3.0.0

// Install FluentValidationForMassTransit as a Cake Tool
#tool nuget:?package=FluentValidationForMassTransit&version=3.0.0

FluentValidationForMassTransit

Allows functionality from the FluentValidation libraries to used in a GreenPipes (MassTransit) pipeline. This means that any messages (e.g. commands and queries) that pass through your pipeline will be validated if a validator exists for that message type, otherwise they won't.

Get Started

  1. Install the Nuget package FluentValidationForMassTransit
  2. In your Startup.cs file, in your ConfigureServices method, add FluentValidation and register your validators as per the FluentValidation documentation:
       services.AddControllers()
           .AddFluentValidation(configuration => configuration
           .RegisterValidatorsFromAssemblyContaining<SomeValidator>());
  1. Decide what you would like to happen when a message fails validation. Make a ValidationFailurePipe to handle those messages. Your ValidationFailurePipe must implement FluentValidationForMassTransit.IValidationFailurePipe (an interface included in this package). It can optionally inherit from FluentValidationForMassTransit.ValidationFailurePipeBase (a base class included in this package). Here is an example of a ValidationFailurePipe that passes the dictionary of validation errors back to the caller, but you can code whatever functionality you like. In most cases you'll want to be calling context.InnerContext.RespondAsync. The context's InnerContext is the ConsumeContext of the message that was validated.
public class ValidationFailurePipe<TMessage> : ValidationFailurePipeBase<TMessage>
    where TMessage : class
{

    public async override Task Send(ValidationFailureContext<TMessage> context)
    {
        var validationProblems = context.ValidationProblems;
        await context.InnerContext.RespondAsync(validationProblems);
    }
}
  1. Register your ValidationFailurePipe in Startup.ConfigureServices with a transient lifetime.
        services.AddTransient(
            typeof(IValidationFailurePipe<>),
            typeof(ValidationFailurePipe<>));
  1. In Startup.ConfigureServices, when you call AddMassTransit, you will then specify your transport mode on the IServiceCollectionBusConfigurator such as UsingInMemory or UsingRabbitMQ etc. Through that, you can use the fluent API to get an instance of IReceiveEndpointConfigurator. That is where you can specify (using the extension method in this package) UseFluentValidationForMassTransit. Pass as an arguemnt your instance of IBusRegistrationContext. An example is below:
  services.AddMassTransit(busConfigurator =>
            {
                busConfigurator.UsingRabbitMq((busContext, rabbitMQConfigurator) =>
                {
                    // Add consumers
                    // Add request clients

                    rabbitMQConfigurator.ReceiveEndpoint(AssemblyName, endpointConfigurator =>
                    {
                        endpointConfigurator.UseFluentValidationForMassTransit(busContext);
                        // Configure consumers
                    }
                }
            })
            .AddMassTransitHostedService(true);
  1. From now on, if the FluentValidation library finds a validator that matches the message type, it will use it to validate the message. If a message passes validation, it will be sent to the next handler in the pipeline. If it fails, it will be passed to your ValidationFailurePipe to be handled. Below is an example of a validator:
public class SomeValidator : AbstractValidator<ISomeCommand>
{
    public SomeValidator()
    {
        RuleFor(x => x.OrderId).NotEmpty().WithMessage(command =>
        $"'{command.OrderId}' is not a valid identifier.");

        RuleFor(x => x.Items).NotEmpty().WithMessage(command =>
        $"Order { command.OrderId} has no items.");
    }
}
Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp2.0 netcoreapp2.1 netcoreapp2.2 netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.0 netstandard2.1
.NET Framework net461 net462 net463 net47 net471 net472 net48 net481
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen40 tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.0.0 1,702 8/5/2022
2.0.0 264 8/5/2022
1.0.0 530 12/4/2021

Upgraded to MassTransit 8.0