Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

.net Core3.0 how to use gRPC

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

Net Core3.0 how to use gRPC, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.

What is gRPC?

GRPC is a modern open source high-performance RPC framework that can run in any environment. It can effectively connect services within and across data centers through pluggable support for load balancing, tracking, health checking, and authentication. It also applies to the last mile of distributed computing to connect devices, mobile applications and browsers to back-end services.

Proto file

The contract used to define gRPC services and messages; the server and client share proto files.

Create a gRPC server using the new template

The. Net core 3.0 creation project provides a new gRPC template that makes it easy to build gRPC services using ASP.NET Core. We follow the steps to create the AA.GrpcService service step by step, of course you can use the command: dotnet new grpc-o GrpcGreeter

Select a gRPC service project template

The final generated project

Greet.proto file syntax = "proto3"

Option csharp_namespace = "AA.GrpcService"

Package Greet

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

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

/ / The response message containing the greetings.message HelloReply {string message = 1;} GreeterService.cs

Public class GreeterService: Greeter.GreeterBase {private readonly ILogger _ logger; public GreeterService (ILogger logger) {_ logger = logger;}

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

Public void ConfigureServices (IServiceCollection services) {services.AddGrpc ();}

Public void Configure (IApplicationBuilder app, IWebHostEnvironment env) {if (env.IsDevelopment ()) {app.UseDeveloperExceptionPage ();}

App.UseRouting ()

App.UseEndpoints (endpoints = > {endpoints.MapGrpcService ()

Endpoints.MapGet ("/", async context = > {await context.Response.WriteAsync ("Communication with gRPC endpoints must be made through a gRPC client To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");});});}

After the creation is completed, the reference to the package, the creation of the proto file, and the generation of the services service are automatically included. The template project performs some operations in the background, such as

Create an ASP.NET Core project that contains all gRPC dependencies.

Create a gRPC service definition file named greet.proto.

All gRPC stubs are automatically generated based on the service definition file.

GreeterService.cs creates a gRPC service based on an automatically generated gRPC stub.

Configure gRPC pipe mapping to GreeterService.cs in Startup.cs

Run the service

To create a gRPC client, we create a console application that invokes the gRPC service as the client

To reference the gRPC service, step: right-click the project to add = "Service reference pops up the following page

Click OK

When we look at the project structure, they will automatically help us with the operation:

Add a reference package:

Package Grpc.Net.ClientFactory

Package Google.Protobuf

Package Grpc.Tools

Protos files (including greet.proto) are automatically copied from the AA.GrpcService project

Add nodes automatically

Protos\ greet.proto finally, add the following code to make the gRPC request

Class Program {static async Task Main (string [] args) {using var channel = GrpcChannel.ForAddress ("https://localhost:5005"); var client = new Greeter.GreeterClient (channel); var response = await client.SayHelloAsync (new HelloRequest {Name =" gRPC "}); Console.WriteLine (" Greeting: "+ response.Message); Console.WriteLine (" Press a key to exit ") Console.ReadKey ();}}

Run result diagram:

This is the answer to the question about how .net Core3.0 uses gRPC. I hope the above content can be of some help to you. If you still have a lot of questions to solve, you can follow the industry information channel to learn more about it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report