Codify.GrpcCodeFirstDataAnnotations.Exceptions 1.0.5

dotnet add package Codify.GrpcCodeFirstDataAnnotations.Exceptions --version 1.0.5
                    
NuGet\Install-Package Codify.GrpcCodeFirstDataAnnotations.Exceptions -Version 1.0.5
                    
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="Codify.GrpcCodeFirstDataAnnotations.Exceptions" Version="1.0.5" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Codify.GrpcCodeFirstDataAnnotations.Exceptions" Version="1.0.5" />
                    
Directory.Packages.props
<PackageReference Include="Codify.GrpcCodeFirstDataAnnotations.Exceptions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Codify.GrpcCodeFirstDataAnnotations.Exceptions --version 1.0.5
                    
#r "nuget: Codify.GrpcCodeFirstDataAnnotations.Exceptions, 1.0.5"
                    
#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.
#:package Codify.GrpcCodeFirstDataAnnotations.Exceptions@1.0.5
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Codify.GrpcCodeFirstDataAnnotations.Exceptions&version=1.0.5
                    
Install as a Cake Addin
#tool nuget:?package=Codify.GrpcCodeFirstDataAnnotations.Exceptions&version=1.0.5
                    
Install as a Cake Tool

GRPC Code-First Data Annotations

Data annotation validation for gRPC Code-First models in ASP.NET Core. Automatically validates request messages using System.ComponentModel.DataAnnotations attributes and returns structured validation errors via gRPC trailers. Optionally validate required reference type properties on request models.

Packages

Package Description
Codify.GrpcCodeFirstDataAnnotations Server-side interceptor that validates incoming gRPC requests
Codify.GrpcCodeFirstDataAnnotations.Exceptions Client-side helper to read validation errors from gRPC RpcException trailers

Getting Started

Server Setup

  1. Install the NuGet package:
dotnet add package Codify.GrpcCodeFirstDataAnnotations
  1. Register services and enable the validation interceptor:
var builder = WebApplication.CreateBuilder(args);

builder.Services.AddGrpcDataAnnotationValidation();
builder.Services.AddCodeFirstGrpc(options =>
{
    options.EnableDataAnnotationValidation();
});

Optionally configure the validation behavior:

builder.Services.Configure<GrpcDataAnnotationValidationOptions>(options =>
{
    // In addition to DataAnnotations attributes, also validate `required` nullable reference type properties
    options.ValidateRequiredNullableProperties = true;

    // Log validation failures at Warning level
    options.ValidationFailureLogLevel = LogLevel.Warning;
    
});
  1. Add DataAnnotations attributes to your request models:
[DataContract]
public class CreatePersonRequest
{
    [DataMember(Order = 1)]
    [Required]
    [StringLength(50)]
    public string FirstName { get; set; }

    [DataMember(Order = 2)]
    [Required]
    [EmailAddress]
    public string Email { get; set; }

    [DataMember(Order = 3)]
    [Range(1, 120)]
    public int Age { get; set; }

    [DataMember(Order = 4)]
    [Range(typeof(TimeSpan), "00:30:00", "08:00:00")]
    public TimeSpan SessionDuration { get; set; }

    [DataMember(Order = 5)]
    // This property will be validated as well if `ValidateRequiredNullableProperties` is enabled, even without DataAnnotations attributes
    public required string Job { get; set; }
}

When a request fails validation, the interceptor throws an RpcException with StatusCode.InvalidArgument and includes the validation errors as Base64-encoded JSON in the gRPC trailers.

Client Setup

  1. Install the client package:
dotnet add package Codify.GrpcCodeFirstDataAnnotations.Exceptions
  1. Catch and read validation errors:
try
{
    var response = await client.CreatePersonAsync(request);
}
catch (RpcException ex)
{
    var errors = ex.GetValidationErrors();
    foreach (var error in errors)
    {
        Console.WriteLine($"{string.Join(", ", error.PropertyNames)}: {error.ErrorMessage}");
    }
}

Custom Validation Handling

Implement IDataAnnotationsResultHandler to customize how validation failures are processed:

public class MyCustomHandler : IDataAnnotationsResultHandler
{
    public Task<DataAnnotationValidationResult> HandleAsync(IList<ValidationResult> failures)
    {
        // Custom logic here
    }
}

Register it before calling AddGrpcDataAnnotationValidation:

builder.Services.AddSingleton<IDataAnnotationsResultHandler, MyCustomHandler>();

Supported gRPC Method Types

  • Unary
  • Server streaming
  • Client streaming
  • Duplex streaming

For streaming methods, each incoming message is validated as it is read.

License

See license.txt for details.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Codify.GrpcCodeFirstDataAnnotations.Exceptions:

Package Downloads
Codify.GrpcCodeFirstDataAnnotations

Data annotation validations for gRPC Code-first models in AspNetCore

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.0.5 105 5/12/2026
1.0.4 97 5/12/2026
1.0.1 104 5/11/2026
1.0.0 97 5/11/2026