DataAnnotations message validation

Project Hosting
NuGet Package NServiceBus.DataAnnotations (1.x) | License
This is a community run project. License and support are independent of Particular Software.
Target NServiceBus Version: 7.x

Uses System.ComponentModel.DataAnnotations to validate incoming and outgoing messages.

DataAnnotations message validation can be enabled using the following:

endpointConfiguration.UseDataAnnotationsValidation();

This will result in, when an invalid message being detected, a validation exception being thrown and that message being handled by Recoverability. The validation exception will also be added to Unrecoverable exceptions to avoid unnecessary retries.

By default, incoming and outgoing messages are validated.

To disable for incoming messages use the following:

endpointConfiguration.UseDataAnnotationsValidation(
    incoming: false);

To disable for outgoing messages use the following:

endpointConfiguration.UseDataAnnotationsValidation(
    outgoing: false);

Enabling validation on outgoing message will result in the validation exception be thrown in the context of the sender, instead of during message processing on the receiving endpoint. This can be particularly helpful in development and/or debugging scenarios since the stack trace and debugger will more accurately reflect the cause of the invalid message.

Messages can then be decorated with DataAnnotations attributes. For example, to make a property required use the RequiredAttribute:

public class MyMessage : IMessage
{
    [Required]
    public string Content { get; set; }
}

Last modified