Fluent Validation

Component: FluentValidation Message Validator
NuGet Package NServiceBus.FluentValidation (1.x)
This is a community maintained project
Target NServiceBus Version: 7.x

Uses the NServiceBus.FluentValidation component to validate various properties of the incoming message and outgoing messages.

Code walk-through

Adding the fluent message validator

In the snippet below, a fluent AbstractValidator for the type MyMessage is created with a rule to check that the Content property of the message is not empty.

public class MyMessageValidator : AbstractValidator<MyMessage>
    public MyMessageValidator()
        RuleFor(_ => _.Content).NotEmpty();

Configuring the endpoint

The validator is then registered in the endpoint configuration as shown in the snippet below. The validation used in this sample is for incoming messages only, so the validator configuration is disabled for outgoing messages. Validators are scanned on start up and registered and resolved using the container.

var validation = configuration.UseFluentValidation(outgoing: false);

Running the project

  1. Start the solution.
  2. Two messages of type, MyMessage are automatically sent to the endpoint on start up. The first message has a value for the Content property and the second message has an empty value.
  3. The handler for the message whose Content property has been set will be handled correctly while a validation exception will be thrown for the message whose Content property is empty.

Last modified