gRPC for .NET

repository·master·Indexed 26 days ago

https://github.com/grpc/grpc-dotnet

A high-performance, modern RPC framework for .NET Core 3.0+ and the recommended implementation for C# developers. It provides seamless integration with ASP.NET Core and HttpClient through core packages including Grpc.AspNetCore, Grpc.Net.Client, and Grpc.Net.ClientFactory. The framework supports gRPC-Web for browser clients, health checking via Grpc.AspNetCore.HealthChecks, and authentication through Grpc.Auth.

Tokens
11.8K
Snippets
30
Records
75
Agent score
86%

What's inside grpc-dotnet

  1. Core gRPC for .NET Packages

    master

    gRPC for .NET provides high-performance RPC functionality for .NET Core 3.0 or later. The implementation is divided into three primary packages:

    • Grpc.AspNetCore: An ASP.NET Core framework for hosting gRPC services. It integrates with standard ASP.NET Core features like logging, dependency injection (DI), authentication, and authorization.
    • Grpc.Net.Client: A gRPC client for .NET Core that builds upon HttpClient and utilizes HTTP/2 functionality.
    • Grpc.Net.ClientFactory: Provides integration with HttpClientFactory, allowing gRPC clients to be centrally configured and injected via Dependency Injection.
  2. Use the Grpc.HealthCheck reference implementation

    master
    Grpc.HealthCheck provides the reference implementation for the gRPC health check service. This allows you to implement a standard health check service that can be queried by external monitoring tools or load balancers to determine the status of your gRPC service.
  3. Use Grpc.Reflection for gRPC reflection service

    master
    Grpc.Reflection provides the reference implementation of the gRPC reflection service. This service allows clients to query a gRPC server for information about the services it hosts, including method names, request/response types, and service definitions. This is commonly used by tools like grpcui, grpcurl, or Postman to discover server capabilities dynamically.
  4. Understand gRPC ASP.NET Core package structure

    master

    The gRPC implementation for ASP.NET Core is provided through managed packages designed to work seamlessly as both client and server. The core components include:

    • Grpc.AspNetCore: A metapackage providing the managed server and client experience, including dependencies on Grpc.Tools (for code generation) and Google.Protobuf (for the default message protocol).
    • Grpc.Core.Api: A purely managed package containing extracted APIs from the original Grpc.Core to allow for a managed server implementation without native dependencies.

    Note that package names are prefixed with Grpc.AspNetCore/ or Grpc.NetCore to distinguish them from native clients and servers.

  5. Use Code-First gRPC with protobuf-net

    master
    As an alternative to standard Protobuf contract definition, you can use the protobuf-net.Grpc external library. This allows you to define Protobuf contracts directly in C# code rather than using .proto files.
  6. Understand gRPC-dotnet versioning policy

    master

    The grpc-dotnet packages follow a specific versioning scheme to maintain compatibility with the broader gRPC ecosystem:

    • Major version: Incremented only when there are breaking changes necessitated by major updates to Grpc.Core or ASP.NET Core dependencies.
    • Minor version: Matches the minor version number of the corresponding Grpc.Core release schedule.
    • Patch version: Increments for bug fixes and patches within an existing major.minor release.

    Affected Packages:

    • Grpc.AspNetCore
    • Grpc.AspNetCore.Server
    • Grpc.AspNetCore.Server.ClientFactory
    • Grpc.Net.Client
    • Grpc.Net.ClientFactory
    • Grpc.Net.Common
    • dotnet-grpc
  7. Execute gRPC-Web interop tests

    master

    To run the automated interop tests, follow these steps:

    1. Start the InteropTestsWebsite container.
    2. Start the InteropTestsGrpcWebWebsite container.
    3. Navigate to the testassets/InteropTestsGrpcWebWebsite/Tests directory and execute npm test.
    npm test
  8. Integrate HttpClientFactory with grpc-dotnet

    master

    You can integrate grpc-dotnet with HttpClientFactory using the Grpc.Net.ClientFactory NuGet package. This provides:

    • Central configuration of gRPC clients.
    • Dependency injection of clients into your application.
    • Reuse of channel instances.
    • Automatic propagation of cancellation and deadlines when used within a Grpc.AspNetCore hosted service.