You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在.NET Core 2.1及Docker化应用中实现gRPC?求最新技术文档

Implementing gRPC in .NET Core 2.1 + Docker Documentation Recommendations

Hey there! Let’s work through your two questions clearly—first getting gRPC up and running in your .NET Core 2.1 app, then pointing you to reliable, up-to-date resources for Docker + gRPC with .NET.


1. Step-by-Step: Implementing gRPC in a .NET Core 2.1 Application

.NET Core 2.1 doesn’t have the built-in gRPC project templates you’ll find in newer .NET versions, but it’s fully supported via NuGet packages. Here’s how to set it up:

Server Setup

  • Create a base project: Start with an ASP.NET Core 2.1 Web Application (empty or Web API works).
  • Install required NuGet packages: Add these to your server project (stick to Grpc.AspNetCore v2.x series, compatible with .NET Core 2.1):
    Install-Package Grpc.AspNetCore
    Install-Package Grpc.Tools
    Install-Package Google.Protobuf
    
  • Define your gRPC contract: Create a Protos folder in your project, then add a .proto file (e.g., Greeter.proto) with your service and message definitions:
    syntax = "proto3";
    
    option csharp_namespace = "GrpcService";
    
    package greeter;
    
    service Greeter {
      rpc SayHello (HelloRequest) returns (HelloReply);
    }
    
    message HelloRequest {
      string name = 1;
    }
    
    message HelloReply {
      string message = 1;
    }
    
  • Configure your server: Update Startup.cs to enable gRPC:
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddGrpc();
    }
    
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
    
        app.UseRouting();
    
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapGrpcService<GreeterService>(); // Map your gRPC service
            endpoints.MapGet("/", async context =>
            {
                await context.Response.WriteAsync("gRPC server running!");
            });
        });
    }
    
  • Implement the service: Create a class that inherits from the auto-generated base class from your .proto file:
    public class GreeterService : Greeter.GreeterBase
    {
        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = $"Hello, {request.Name}!"
            });
        }
    }
    

Client Setup

  • Create a client project: Use a .NET Core 2.1 Console App or any other project type.
  • Install client packages:
    Install-Package Grpc.Net.Client
    Install-Package Google.Protobuf
    Install-Package Grpc.Tools
    
  • Link the .proto file: Add a reference to your server’s Greeter.proto file in the client’s project file (so Grpc.Tools generates client code):
    <ItemGroup>
      <Protobuf Include="..\GrpcService\Protos\Greeter.proto" GrpcServices="Client" />
    </ItemGroup>
    
  • Call the gRPC service: Write client code to connect and invoke the service:
    using var channel = GrpcChannel.ForAddress("https://localhost:5001");
    var client = new Greeter.GreeterClient(channel);
    var reply = await client.SayHelloAsync(new HelloRequest { Name = "Docker + gRPC User" });
    Console.WriteLine($"Received: {reply.Message}");
    

2. Up-to-Date Documentation for Docker + .NET Core 2.1 gRPC

It’s true that some official docs lean into newer .NET versions, but these resources are still reliable for your stack:

  • Official .NET gRPC Docs (Version-Filtered): Head to the .NET gRPC documentation, use the version selector at the top to pick .NET Core 2.1. The Docker deployment section covers writing a Dockerfile for gRPC, configuring TLS (critical for gRPC), and exposing the correct ports (default gRPC port is 5001 for HTTPS).
  • gRPC Official Docker Guide: The gRPC team’s general Docker guide has best practices for containerizing gRPC services, which apply directly to .NET Core 2.1. Focus on sections about network configuration, health checks, and avoiding common pitfalls like unencrypted connections.
  • GitHub Sample Repos: Check the grpc-dotnet repository on GitHub—filter to the 2.x branch (compatible with .NET Core 2.1) and look in the samples folder. You’ll find working Dockerfile examples for both server and client, plus Docker Compose setups for multi-service scenarios.
  • Stack Overflow Threads: Search for tags like [grpc] [.net-core-2.1] [docker]—you’ll find real-world solutions to issues like container-to-container gRPC communication, certificate mounting in Docker, and optimizing image sizes for .NET Core 2.1.

内容的提问来源于stack exchange,提问作者Harsh Nagalla

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.11 09:03:53