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 Core micro service rpc framework how to use GRPC communication

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

In this article Xiaobian introduces in detail ".net Core micro-service rpc framework GRPC communication how to use", the content is detailed, the steps are clear, the details are handled properly, I hope this ".net Core micro-service rpc framework GRPC communication how to use" article can help you solve doubts, the following follow the editor's ideas slowly in-depth, together to learn new knowledge.

Server side

First, take out the previously written proto file, and then modify two properties:

Build Action = > Protobuf compilergRpc Stub Classes = > Server only

As shown in the figure:

Of course, you can also see it in the project file:

The project is then rebuilt, and the server-side file is automatically generated from the proto file.

Quote

After just now, the corresponding service has been generated, and we can call it directly in the code.

This is the proto written before:

Syntax = "proto3"; option csharp_namespace = "gRPCApiDemo.Protos"; package Demo;service MyMath {rpc MathAdd (AddRequest) returns (AddRespones) {} message AddRequest {int32 astat1; int32 bread2;} message AddRespones {int32 astat1;}

After generation, there will be a MyMath.MyMathBase class, which we will inherit:

Notice the namespace, which was generated according to proto after the project was built just now.

Now let's rewrite the method (the following figure is generated, or you can write it manually):

According to the proto file:

AddRequest contains two int parameters: an and B.

AddRespones contains an int parameter named A

So let's add up the AB and return:

Using Grpc.Core;using gRPCApiDemo.Protos;using System;using System.Collections.Generic;using System.Linq;using System.Threading.Tasks;namespace gRPCApiDemo.Grpc {public class GrpcServiceMath: MyMath.MyMathBase {public override Task MathAdd (AddRequest request, ServerCallContext context) {var respones = new AddRespones {A = request.A + request.B}; return Task.FromResult (respones) }}}

Then go to StartUp and set it:

App.UseHttpsRedirection (); app.UseEndpoints (endpoints = > {endpoints.MapGrpcService ();})

This is the end of the service.

If you write more service, you need to declare more implementation classes here; and https is a must.

Client

I have prepared a blank project. Next, you can copy the proto file from the previous server, or choose to write a new one, then modify the properties and generate the project:

In fact, another option is Client and Server, which generates both the client and the server at once.

Then inject the soul:

Services.AddGrpcClient (o = > o.Address = new Uri ("https://localhost:5001"));)

MyMath is the service declared in proto. MyMathClient was just generated, and the Uri in it is the domain name where the server is located.

Because gRpc is based on http/2, there is a troublesome certificate to do if you want to access it through http/2. If you don't want to do it, you can add this line:

AppContext.SetSwitch ("System.Net.Http.SocketsHttpHandler.Http2UnencryptedSupport", true)

Of course, don't forget the following https settings.

Then we create a new controller and call the method directly:

Public class IndexController: Controller {private readonly MyMath.MyMathClient _ client; public IndexController (MyMath.MyMathClient client) {this._client = client } public async Task Add (int a, int b) {var respones = await _ client.MathAddAsync (new AddRequest () {A = a, B = b}); return respones.A;}}

MyMathClient, like MyMathBase, is automatically generated. And now this version automatically generates an asynchronous version of the rpc call, just like MathAddAsync in the code.

Let's run and see:

Perfect.

Source code address

As a final reminder, server and client must have https, otherwise:

After reading this, the article "how to use .net Core rpc framework GRPC communication" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it. If you want to know more about the article, please follow the industry information channel.

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

Development

Wechat

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

12
Report