FluentValidationForMassTransit 3.0.0
.NET 6.0
This package targets .NET 6.0. The package is compatible with this framework or higher.
.NET Standard 2.0
This package targets .NET Standard 2.0. The package is compatible with this framework or higher.
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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
- Install the Nuget package
FluentValidationForMassTransit
- 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>());
- Decide what you would like to happen when a message fails validation. Make a
ValidationFailurePipe
to handle those messages. YourValidationFailurePipe
must implementFluentValidationForMassTransit.IValidationFailurePipe
(an interface included in this package). It can optionally inherit fromFluentValidationForMassTransit.ValidationFailurePipeBase
(a base class included in this package). Here is an example of aValidationFailurePipe
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 callingcontext.InnerContext.RespondAsync
. The context'sInnerContext
is theConsumeContext
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);
}
}
- Register your
ValidationFailurePipe
inStartup.ConfigureServices
with a transient lifetime.
services.AddTransient(
typeof(IValidationFailurePipe<>),
typeof(ValidationFailurePipe<>));
- In
Startup.ConfigureServices
, when you callAddMassTransit
, you will then specify your transport mode on theIServiceCollectionBusConfigurator
such asUsingInMemory
orUsingRabbitMQ
etc. Through that, you can use the fluent API to get an instance ofIReceiveEndpointConfigurator
. That is where you can specify (using the extension method in this package)UseFluentValidationForMassTransit
. Pass as an arguemnt your instance ofIBusRegistrationContext
. 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);
- 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 Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 was computed. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.0
- FluentValidation (>= 11.1.0)
- MassTransit (>= 8.0.5)
-
.NETStandard 2.1
- FluentValidation (>= 11.1.0)
- MassTransit (>= 8.0.5)
-
net6.0
- FluentValidation (>= 11.1.0)
- MassTransit (>= 8.0.5)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Upgraded to MassTransit 8.0