Codify.GrpcCodeFirstDataAnnotations
1.0.0
See the version list below for details.
dotnet add package Codify.GrpcCodeFirstDataAnnotations --version 1.0.0
NuGet\Install-Package Codify.GrpcCodeFirstDataAnnotations -Version 1.0.0
<PackageReference Include="Codify.GrpcCodeFirstDataAnnotations" Version="1.0.0" />
<PackageVersion Include="Codify.GrpcCodeFirstDataAnnotations" Version="1.0.0" />
<PackageReference Include="Codify.GrpcCodeFirstDataAnnotations" />
paket add Codify.GrpcCodeFirstDataAnnotations --version 1.0.0
#r "nuget: Codify.GrpcCodeFirstDataAnnotations, 1.0.0"
#:package Codify.GrpcCodeFirstDataAnnotations@1.0.0
#addin nuget:?package=Codify.GrpcCodeFirstDataAnnotations&version=1.0.0
#tool nuget:?package=Codify.GrpcCodeFirstDataAnnotations&version=1.0.0
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
required properties then returns structured validation errors via gRPC trailers.
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
- Install the NuGet package:
dotnet add package Codify.GrpcCodeFirstDataAnnotations
- Register services and enable the validation interceptor:
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddGrpcDataAnnotationValidation();
builder.Services.AddCodeFirstGrpc(options =>
{
options.EnableDataAnnotationValidation();
});
- Add
DataAnnotationsattributes 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)]
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
- Install the client package:
dotnet add package Codify.GrpcCodeFirstDataAnnotations.Exceptions
- 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 | Versions 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. |
-
net10.0
- Codify.GrpcCodeFirstDataAnnotations.Exceptions (>= 1.0.0)
- Grpc.AspNetCore (>= 2.80.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.