Table of Contents

Validating the Consumed Messages

The messages are validated using the same mechanism implemented in the ASP.NET Core controllers.

You can either decorate the message model with the System.ComponentModel.DataAnnotations.ValidationAttribute standard implementations, create your own attributes (extending ValidationAttribute) or otherwise you can implement the IValidatableObject interface in your message model.

Logging Invalid Messages

By default, the validation is enabled and the MessageValidationMode is set to LogWarning. This means that a warning is logged if the consumed message is not valid.

Preventing Invalid Messages from Being Processed

If you want to prevent invalid messages from being processed, you can set the MessageValidationMode to ThrowException. This way, an exception will be thrown when the message is received and the processing will fail. Silverback will then handle the exception according to the specified error policy (see Consuming Messages).

services.AddSilverback()
    .WithConnectionToMessageBroker(options => options.AddKafka())
    .AddKafkaClients(clients => clients
        .WithBootstrapServers("PLAINTEXT://localhost:9092")
        .AddConsumer("consumer1", consumer => consumer
            .Consume<MyMessage>("endpoint1", endpoint => endpoint
                .ConsumeFrom("my-topic")
                .ValidateMessage(throwException: true))));

Disabling Validation

Validation can affect performance, with the impact varying based on object size, the number of validations, and their complexity. If performance is a critical concern for your use case, you may consider disabling validation.

If your models don't have any validation attributes, no validation will be performed by default. However, you can explicitly disable the validation using the DisableMessageValidation method.

services.AddSilverback()
    .WithConnectionToMessageBroker(options => options.AddKafka())
    .AddKafkaClients(clients => clients
        .WithBootstrapServers("PLAINTEXT://localhost:9092")
        .AddConsumer("consumer1", consumer => consumer
            .Consume<MyMessage>("endpoint1", endpoint => endpoint
                .ConsumeFrom("my-topic")
                .DisableMessageValidation())));

Additional Resources