将 WCF 与 gRPC 进行比较Comparing WCF to gRPC

上一章介绍了 Protobuf 和 gRPC 如何处理消息。The previous chapter gave you a good look at Protobuf and how gRPC handles messages. 在处理从 Windows Communication Foundation (WCF)到 gRPC 的详细转换之前,请务必了解 WCF 中的功能在 gRPC 中的处理方式,以及在没有 gRPC 等效项时可以使用的解决方法。Before you work through a detailed conversion from Windows Communication Foundation (WCF) to gRPC, it's important know how the features available in WCF are handled in gRPC and what workarounds you can use when there's no gRPC equivalent. 具体而言,本章将涵盖以下主题:In particular, this chapter will cover the following subjects:

  • 操作和方法Operations and methods
  • 绑定和传输Bindings and transports
  • RPC 类型RPC types
  • 元数据Metadata
  • 错误处理。Error handling
  • WS* 协议WS-* protocols

gRPC 示例gRPC example

当你从 Visual Studio 2019 或命令行创建新的 ASP.NET Core 3.0 gRPC 项目时,将为你生成 gRPC 等效的 "Hello World"。When you create a new ASP.NET Core 3.0 gRPC project from Visual Studio 2019 or the command line, the gRPC equivalent of "Hello World" is generated for you. 它包含一个用于定义服务及其消息的 greeter.proto 文件,以及一个包含服务实现的 GreeterService.cs 文件。It consists of a greeter.proto file that defines the service and its messages, and a GreeterService.cs file with an implementation of the service.

syntax = "proto3";

option csharp_namespace = "HelloGrpc";

package Greet;

// The greeting service definition.
service Greeter {
  // Sends a greeting
  rpc SayHello (HelloRequest) returns (HelloReply);
}

// The request message that contains the user's name.
message HelloRequest {
  string name = 1;
}

// The response message that contains the greetings.
message HelloReply {
  string message = 1;
}
using System.Threading.Tasks;
using Grpc.Core;
using Microsoft.Extensions.Logging;

namespace HelloGrpc
{
    public class GreeterService : Greeter.GreeterBase
    {
        private readonly ILogger<GreeterService> _logger;
        public GreeterService(ILogger<GreeterService> logger)
        {
            _logger = logger;
        }

        public override Task<HelloReply> SayHello(HelloRequest request, ServerCallContext context)
        {
            return Task.FromResult(new HelloReply
            {
                Message = "Hello " + request.Name
            });
        }
    }
}

本章介绍了 gRPC 的不同概念和功能。This chapter will refer to this example code when explaining different concepts and features of gRPC.