GRPC. NET Core cross-platform learning, grpc. netcore
GRPC C # learning was released the other day, And gRPC is used in. NET Framework. Today we will learn about. NET Core gRPC.
The. NET Core package of gRPC is released on NuGet, And the. NET Core is used in combination to implement a perfect cross-platform gRPC.
This article describes how the. NET Core gRPC client runs on the Ubuntu system and communicates with the server in the LAN.
Now let's officially start.
In the previous basic code development. NET Core version.
Running environment of this article:
Server: WIN10 x64
Client: Ubuntu 14.04
Add. NET Core gRPC Client
First we open the previous gRPCDemo code, GitHub: https://github.com/linezero/Blog/tree/master/gRPCDemo.
Add a. NET Core console application gRPCNETCoreClient
Then add a reference to the gRPCNETCoreClient project:
Install-Package Grpc -Pre
Porting traditional class libraries to. NET Core Class Libraries
After Grpc is installed, we can transplant the Class Library. Here we first create a gRPCNETCoreDemo Class Library.
After the project is created, change the project. json file to the following:
{ "version": "1.0.0-*", "frameworks": { "net452": { "dependencies": { "Grpc": "1.0.0", "Grpc.Core": "1.0.0", "Google.Protobuf": "3.0.0", "System.Interactive.Async": "3.0.0" } }, "netstandard1.6": { "imports": "dnxcore50", "dependencies": { "NETStandard.Library": "1.6.0", "Grpc": "1.0.1-pre1", "Grpc.Core": "1.0.1-pre1", "Google.Protobuf": "3.1.0", "System.Interactive.Async": "3.1.0-rc" } } }}
In this way, the Class Library supports. NET Framework 4.5.2 and. NET Core.
Here I omitted the generated code and copied the class in gRPCDemo directly. Then add the reference gRPCNETCoreDemo to gRPCNETCoreClient.
In gRPCNETCoreClient, add the following code to Program. cs, which is basically the same as the previous code. The console encoding output format is added.
Public class Program {public static void Main (string [] args) {Encoding. registerProvider (CodePagesEncodingProvider. instance); Channel channel = new Channel ("127.0.0.1: 9007", ChannelCredentials. insecure); var client = new gRPC. gRPCClient (channel); var reply = client. sayHello (new HelloRequest {Name = "LineZero. NET Core Client "}); Console. writeLine ("from" + reply. message); channel. shutdownAsync (). wait (); Console. writeLine ("any key to exit... "); Console. readKey ();}}
After the code is compiled, execute gRPCServer, and then run gRPCNETCoreClient using dotnet run.
Successful communication proves that. NET Core is supported.
Release to Ubuntu for running
Next we will release gRPCNETCoreClient to the Ubuntu system for cross-platform running.
The ip address bound to the Code must be slightly adjusted.
Check the local ip address, change localhost in gRPCServer to the local ip address, and change 127.0.0.1 of the Client to the local ip address.
Release gRPCNETCoreClient
Dotnet publish
Copy the released directory to the Ubuntu system.
Next, start the Server locally and then execute the Client in the Ubuntu system.
Communication is successful on the Ubuntu system. Here, the server is running locally and the client is on another machine.
If you think this article is helpful to you, click"Recommendation", Thank you.