This was because the validation were getting executed as a Global RequestFilter which aren't executed for MQ Requests which are considered to be internal services and skip the Global Request Filters.
We've added new IAppHost.GlobalMessageRequestFilters
in v4.05 which the ValidationFeature uses to now validate requests via MQ as well.
Note: that any validation or service errors are sent to the Response DLQ (e.g. QueueNames<RegisterCustomerResponse>.Dlq
) as opposed to the default INQ (e.g. QueueNames<RegisterCustomerResponse>.Inq
) for valid responses as visible in this example:
public class ValidateTestMq
{
public int Id { get; set; }
}
public class ValidateTestMqResponse
{
public int CorrelationId { get; set; }
public ResponseStatus ResponseStatus { get; set; }
}
public class ValidateTestMqValidator : AbstractValidator<ValidateTestMq>
{
public ValidateTestMqValidator()
{
RuleFor(x => x.Id)
.GreaterThanOrEqualTo(0)
.WithErrorCode("PositiveIntegersOnly");
}
}
The test below publishes an invalid request followed by a valid one:
using (var mqFactory = appHost.TryResolve<IMessageFactory>())
{
var request = new ValidateTestMq { Id = -10 };
mqFactory.CreateMessageProducer().Publish(request);
var msg = mqFactory.CreateMessageQueueClient()
.Get(QueueNames<ValidateTestMqResponse>.Dlq, null)
.ToMessage<ValidateTestMqResponse>();
Assert.That(msg.GetBody().ResponseStatus.ErrorCode,
Is.EqualTo("PositiveIntegersOnly"));
request = new ValidateTestMq { Id = 10 };
mqFactory.CreateMessageProducer().Publish(request);
msg = mqFactory.CreateMessageQueueClient()
.Get(QueueNames<ValidateTestMqResponse>.In, null)
.ToMessage<ValidateTestMqResponse>();
Assert.That(msg.GetBody().CorrelationId, Is.EqualTo(request.Id));
}