Fluent.Unions.Grpc
1.1.0
dotnet add package Fluent.Unions.Grpc --version 1.1.0
NuGet\Install-Package Fluent.Unions.Grpc -Version 1.1.0
<PackageReference Include="Fluent.Unions.Grpc" Version="1.1.0" />
<PackageVersion Include="Fluent.Unions.Grpc" Version="1.1.0" />
<PackageReference Include="Fluent.Unions.Grpc" />
paket add Fluent.Unions.Grpc --version 1.1.0
#r "nuget: Fluent.Unions.Grpc, 1.1.0"
#:package Fluent.Unions.Grpc@1.1.0
#addin nuget:?package=Fluent.Unions.Grpc&version=1.1.0
#tool nuget:?package=Fluent.Unions.Grpc&version=1.1.0
![]()
Fluent.Unions.Grpc
Converts gRPC client calls to Result<T>, so an RpcException becomes an error. On the server, a Grpc.Core interceptor turns the error of a result into an RpcException with a status code. It builds on Fluent.Unions and Grpc.Core.Api (Apache-2.0), so it works with Grpc.Net.Client and Grpc.AspNetCore.
Installation
dotnet add package Fluent.Unions.Grpc
Usage
Client: convert the call.
using Fluent.Unions.Grpc;
Result<UserReply> user = await client.GetUserAsync(new UserRequest { Id = 7 }).ToResult();
// the reply, or an RpcException with the status code only as the error
if (user.TryGetError(out var error) && error is RpcException { StatusCode: StatusCode.NotFound })
// handle it
Server: a response is a protobuf message, not a Result, so the service method unwraps the result with Check() and the interceptor maps the error.
builder.Services.AddGrpc(grpc => grpc.Interceptors.Add<ResultServerInterceptor>());
public override async Task<UserReply> GetUser(UserRequest request, ServerCallContext context) =>
(await users.Find(request.Id)).Check().GetValue();
KeyNotFoundException becomes NotFound, a ValidationError or ValidationErrors becomes InvalidArgument with its messages and member names. An RpcException is passed on as it is. Map your own errors with ResultGrpcOptions, configured with the options pattern:
builder.Services.Configure<ResultGrpcOptions>(options => options.Map<OutOfStockException>(StatusCode.FailedPrecondition, exposeMessage: true));
error.ToRpcException() does the same mapping by hand.
With your own result type
ToResult() produces the default Fluent.Unions.Result<T>. To get your own type, wrap the call in your own Result.TryAsync(...), or convert with Match. See your own result type.
Shop.Result<UserReply> mine = user.Match(value => (Shop.Result<UserReply>)value, error => DomainException.FromException(error));
Documentation
Rules
- Unary and client-streaming calls.
ToResult()covers calls with a single response. For a streaming response, theRpcExceptioncomes out ofResponseStream.MoveNext. - Only
RpcExceptionis an error. Other exceptions are thrown. The error keeps the status code but not the detail or trailers of the downstream call, so it can be returned without leaking them. The original isStatus.DebugException. - Cancellation is not an error. A call with the status
CancelledthrowsOperationCanceledException, with theRpcExceptionas itsInnerException. - Messages stay hidden. A mapped exception's message reaches the client only with
exposeMessage; validation errors always list theirs. An unmapped exception is rethrown by the interceptor, so gRPC reports it as usual.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Fluent.Unions (>= 1.1.0)
- Grpc.Core.Api (>= 2.85.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.