Sự kiện
23 giờ 19 thg 11 - 23 giờ 21 thg 11
Tham gia các phiên trực tuyến tại Microsoft Ignite được tạo ra để mở rộng kỹ năng của bạn và giúp bạn giải quyết các vấn đề phức tạp ngày nay.
Đăng ký ngayTrình duyệt này không còn được hỗ trợ nữa.
Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.
Lưu ý
This isn't the latest version of this article. For the current release, see the .NET 9 version of this article.
Cảnh báo
This version of ASP.NET Core is no longer supported. For more information, see .NET and .NET Core Support Policy. For the current release, see the .NET 8 version of this article.
Quan trọng
This information relates to a pre-release product that may be substantially modified before it's commercially released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
For the current release, see the .NET 9 version of this article.
This article explains how gRPC services compare to HTTP APIs with JSON (including ASP.NET Core web APIs). The technology used to provide an API for your app is an important choice, and gRPC offers unique benefits compared to HTTP APIs. This article discusses the strengths and weaknesses of gRPC and recommends scenarios for using gRPC over other technologies.
The following table offers a high-level comparison of features between gRPC and HTTP APIs with JSON.
Feature | gRPC | HTTP APIs with JSON |
---|---|---|
Contract | Required (.proto ) |
Optional (OpenAPI) |
Protocol | HTTP/2 | HTTP |
Payload | Protobuf (small, binary) | JSON (large, human readable) |
Prescriptiveness | Strict specification | Loose. Any HTTP is valid. |
Streaming | Client, server, bi-directional | Client, server |
Browser support | No (requires grpc-web) | Yes |
Security | Transport (TLS) | Transport (TLS) |
Client code-generation | Yes | OpenAPI + third-party tooling |
gRPC messages are serialized using Protobuf, an efficient binary message format. Protobuf serializes very quickly on the server and client. Protobuf serialization results in small message payloads, important in limited bandwidth scenarios like mobile apps.
gRPC is designed for HTTP/2, a major revision of HTTP that provides significant performance benefits over HTTP 1.x:
HTTP/2 is not exclusive to gRPC. Many request types, including HTTP APIs with JSON, can use HTTP/2 and benefit from its performance improvements.
All gRPC frameworks provide first-class support for code generation. A core file to gRPC development is the .proto
file, which defines the contract of gRPC services and messages. From this file, gRPC frameworks generate a service base class, messages, and a complete client.
By sharing the .proto
file between the server and client, messages and client code can be generated from end to end. Code generation of the client eliminates duplication of messages on the client and server, and creates a strongly-typed client for you. Not having to write a client saves significant development time in applications with many services.
A formal specification for HTTP API with JSON doesn't exist. Developers debate the best format of URLs, HTTP verbs, and response codes.
The gRPC specification is prescriptive about the format a gRPC service must follow. gRPC eliminates debate and saves developer time because gRPC is consistent across platforms and implementations.
HTTP/2 provides a foundation for long-lived, real-time communication streams. gRPC provides first-class support for streaming through HTTP/2.
A gRPC service supports all streaming combinations:
gRPC allows clients to specify how long they are willing to wait for an RPC to complete. The deadline is sent to the server, and the server can decide what action to take if it exceeds the deadline. For example, the server might cancel in-progress gRPC/HTTP/database requests on timeout.
Propagating the deadline and cancellation through child gRPC calls helps enforce resource usage limits.
gRPC is well suited to the following scenarios:
It's impossible to directly call a gRPC service from a browser today. gRPC heavily uses HTTP/2 features and no browser provides the level of control required over web requests to support a gRPC client. For example, browsers do not allow a caller to require that HTTP/2 be used, or provide access to underlying HTTP/2 frames.
gRPC on ASP.NET Core offers two browser-compatible solutions:
gRPC-Web allows browser apps to call gRPC services with the gRPC-Web client and Protobuf. gRPC-Web requires the browser app to generate a gRPC client. gRPC-Web allows browser apps to benefit from the high-performance and low network usage of gRPC.
.NET has built-in support for gRPC-Web. For more information, see gRPC-Web in ASP.NET Core gRPC apps.
gRPC JSON transcoding allows browser apps to call gRPC services as if they were RESTful APIs with JSON. The browser app doesn't need to generate a gRPC client or know anything about gRPC. RESTful APIs can be automatically created from gRPC services by annotating the .proto
file with HTTP metadata. Transcoding allows an app to support both gRPC and JSON web APIs without duplicating the effort of building separate services for both.
.NET has built-in support for creating JSON web APIs from gRPC services. For more information, see gRPC JSON transcoding in ASP.NET Core gRPC apps.
Lưu ý
gRPC JSON transcoding requires .NET 7 or later.
HTTP API requests are sent as text and can be read and created by humans.
gRPC messages are encoded with Protobuf by default. While Protobuf is efficient to send and receive, its binary format isn't human readable. Protobuf requires the message's interface description specified in the .proto
file to properly deserialize. Additional tooling is required to analyze Protobuf payloads on the wire and to compose requests by hand.
Features such as server reflection and the gRPC command line tool exist to assist with binary Protobuf messages. Also, Protobuf messages support conversion to and from JSON. The built-in JSON conversion provides an efficient way to convert Protobuf messages to and from human readable form when debugging.
Other frameworks are recommended over gRPC in the following scenarios:
Ý kiến phản hồi về ASP.NET Core
ASP.NET Core là một dự án nguồn mở. Chọn liên kết để cung cấp ý kiến phản hồi:
Sự kiện
23 giờ 19 thg 11 - 23 giờ 21 thg 11
Tham gia các phiên trực tuyến tại Microsoft Ignite được tạo ra để mở rộng kỹ năng của bạn và giúp bạn giải quyết các vấn đề phức tạp ngày nay.
Đăng ký ngay