• Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions
Flyy Tech
  • Home
  • Apple
  • Applications
    • Computers
    • Laptop
    • Microsoft
  • Security
  • Smartphone
  • Gaming
  • Entertainment
    • Literature
    • Cooking
    • Fitness
    • lifestyle
    • Music
    • Nature
    • Podcasts
    • Travel
    • Vlogs
  • Camera
  • Audio
No Result
View All Result
  • Home
  • Apple
  • Applications
    • Computers
    • Laptop
    • Microsoft
  • Security
  • Smartphone
  • Gaming
  • Entertainment
    • Literature
    • Cooking
    • Fitness
    • lifestyle
    • Music
    • Nature
    • Podcasts
    • Travel
    • Vlogs
  • Camera
  • Audio
No Result
View All Result
Flyy Tech
No Result
View All Result

Playing with gRPC and .NET 6: Client Side

flyytech by flyytech
September 7, 2022
Home Applications
Share on FacebookShare on Twitter


In my previous article, I focused on how we can create a gRPC server using .NET. Now, I want to show you how we can create a gRPC client using .NET.

In the next image, I mark the items that we will focus on this tutorial.

Prerequisites

I’m using macOS, and the next commands are specific to this OS:

  • gRPC compiler: To install Protobuf compiler, you can execute this in a terminal:
brew install protobuf
  • .NET 6 SDK: Here you can find the links to download and install the .NET 6 SDK
  • Visual Studio Code or IDE of your choice
  • grpcurl: A command-line tool that provides interaction with gRPC services
brew install grpcurl
  • grpcui: builds on top of gRPCurl and adds an interactive web UI for gRPC, similar to tools like Postman and Swagger UI
brew install grpcui

Steps

In a general manner, we should follow the next steps:

  1. Create a project.
  2. Add the gRPC dependencies to the project.
  3. Add the proto file on your project.
  4. Register the created proto file and compile the project.
  5. Implement the business logic.

In this example, we will use a .NET project called CountryGrpcClient that calls to the server to search, create or get a list of countries.  The CountryGrpc.proto file (described below) declares the remote procedures.

1. Create a Project

To create a gRPC template .NET project, open a terminal and execute the next command:

dotnet new console -o grpc.country.client -n CountryGrpcClient

The output is something like this:

Here:

  • -o parameter is used to define the project directory name: grpc.country.client.
  • -n parameter is used to define the project name: CountryGrpcClient.

2. Add the gRPC Dependencies to the Project

dotnet add CountryGrpcClient.csproj package Grpc.Net.Client --version '2.47.0'
dotnet add CountryGrpcClient.csproj package Grpc.Tools --version '2.47.0'
dotnet add CountryGrpcClient.csproj package Google.Protobuf --version '3.21.5'

You can see that the entry ItemGroup in your CountryGrpcClient.csproj has changed to something like this:

3. Add the Proto File in the Project

In your project directory, create a folder called Protos with a file CountryGrpc.protos in it. 

mkdir ./Protos
touch ./Protos/CountryGrpc.proto

In this step, I’ll use the same proto file created in the previous article in which we created a gRPC server.

Copy the next lines in the CountryGrpc.proto file created previously.

syntax = "proto3";

/*The Proto file that has Empty message definition*/
import "google/protobuf/empty.proto";

// Defining the namespace in which the generate classes will be 
option csharp_namespace = "Sumaris.Grpc.Services";

// The service name will be used by the compiler when generate the base classes
// Here I declare five procedure
service CountryService{
	//Server streaming RPC
    rpc getAllCountries(google.protobuf.Empty)
        returns (stream Country);
	// Unitary RPC
    rpc listAllCountries(google.protobuf.Empty)
        returns ( CountryList);
    // Unitary RPC
    rpc findCountryByName( FindCountryByNameRequest )
        returns (FindCountryByNameResponse);
	// Unitary RPC
    rpc createCountry (CountryCreateRequest)
        returns (CountryCreateRespopnse);
	// Bidrectional streaming RPC
    rpc findCountriesByNames( stream FindCountryByNameRequest)
        returns (stream Country);
}


message Country{
    string name=1;
    string capitalCity=2;
    float area=3;
}

message CountryList{repeated Country country = 1;}

message FindCountryByNameRequest{string name=1;}

message FindCountryByNameResponse{Country country=1;}

message CountryCreateRequest{ Country country=1;}

message CountryCreateRespopnse{bool created=1;}

4. Register the Created Proto File and Compile the Project

Add the next lines in your configuration project file, CountryGrpcClient.csproj:

<ItemGroup>
    <Protobuf Include="./Protos/CountryGrpc.proto" GrpcServices="Client" />
</ItemGroup>

Open a terminal, move into the project, and execute the next command to compile the project:

 The build process creates two files, CountryGrpc.cs and CountryGrpcGrpc.cs, in the obj/Debug/net6.0/Protos path.  The file CountryGrpcGrpc.cs contains the class that we will use as a client to interact with the gRPC server.

5. Implement the Business Logic

Open an IDE to edit the Program.cs file and add the code to call the server:

using System.Threading.Tasks;
using Grpc.Net.Client;
using Sumaris.Grpc.Services;
using Google.Protobuf.WellKnownTypes;
using Grpc.Core;


var countryNames = new string[] { "Perú", "Ecuador", "Chile", "Brasil", "Argentina", "Venezuela" };

// See https://aka.ms/new-console-template for more information
using var channel =
    GrpcChannel.ForAddress("http://localhost:5000");
CountryService.CountryServiceClient? client =
    new CountryService.CountryServiceClient(channel);

// Consume unitary
// rpc listAllCountries(google.protobuf.Empty) returns ( CountryList);
var reply = await client.listAllCountriesAsync(new Empty());
Console.WriteLine("Hello, World!");
Console.WriteLine("Greeting: " + reply.Country);


// Consume stream server
//rpc getAllCountries(google.protobuf.Empty) returns (stream Country);
printServerStream();


// Consume unitary
//rpc findCountryByName(FindCountryByNameRequest ) returns(FindCountryByNameResponse);


// Consume client streaming server streaming
//rpc findCountriesByNames( stream FindCountryByNameRequest)  returns (stream Country);
var asyncDuplexStreamingCall = client.findCountriesByNames();
callStreamFromClient(asyncDuplexStreamingCall.RequestStream);
getStreamFromServer(asyncDuplexStreamingCall.ResponseStream);


Console.WriteLine("Press any key to exit...");
Console.ReadKey();



async void printServerStream()
{
    var reply2 = client.getAllCountries(new Empty()).ResponseStream;
    Console.WriteLine("Greeting2: ");
    while (await reply2.MoveNext())
    {
        Console.Write(reply2.Current);
    }
    Console.WriteLine();
}

async void callStreamFromClient(IClientStreamWriter<FindCountryByNameRequest> request)
{
    foreach (var countryName in countryNames)
    {
        var d = new FindCountryByNameRequest(); 
        d.Name = countryName;
        await request.WriteAsync(d);
    }
    await request.CompleteAsync();
}

async void getStreamFromServer(IAsyncStreamReader<Country> response)
{
    await foreach(var p in response.ReadAllAsync())
    {
        Console.Write($">>{response.Current}");
    }

    Console.WriteLine("Terminando ...");
}

In the next images, I want to explain a little more about how you consume the remote procedure. 

The step marked as 1 lets you create the “client” object. You need only one client to interact with the server.

From this point, you use the “client” to call each one of the remote procedures as you can see in step 2. In this case, the client is calling a server streaming RPC called getAllCountries. The server sends the data to the client asynchronously and in streaming fashion (step 3). The client reads the data in streaming fashion too until the server finishes the sending (step 4).

Now that we saw how you can call a streaming server gRPC, I will show you how you can call a bidirectional streaming gRPC.

In this case, we use the same “client” object created previously, step 1, and call the remote bidirectional streaming gRPC. In this example, it is findCountriesByName, which returns an object AsyncServerStreamingCall (step 2). This object wraps two objects: the RequestStream that is used to send streaming data to the server (step 3), and the ResponseStream that is used to get the streaming data returned by the server (step 5). 

The server processes each incoming object sent by the client in the transmission stream, applies its business logic, and asynchronously writes its response to the transmission return stream as we can see in step 4. The client reads the incoming responses sent by the server using the IAsyncStreamReader, as you can see in step 5.

When the client has no more data to send to the server, it must notify the server (red box in step 3), so that the server finishes its process of reading the request asynchronously and can leave the foreach in step 4. In this case, the server ends its process and notifies the client that there is no more data to read. At this point, the client exits the foreach in step 5.

This completes bidirectional transmission.

Now that I have shown you a bidirectional streaming gRPC, you can use it to implement a client streaming gRPC.

Conclusion

We  use the Protocol buffer files to generate the client implementation in .NET and the async/await to manage streaming server or client.  We saw how we can call a streaming server and bidirectional streaming gRPC.

Feel free to let me know if you have any questions or feedback. 

Thank you!



Source_link

flyytech

flyytech

Next Post
Local businesses share how negative, inaccurate reviews hurt them

Local businesses share how negative, inaccurate reviews hurt them

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended.

Cooler Master Jumps the Shark With Pre-Built PCs in Shoe, Shark Cases

Cooler Master Jumps the Shark With Pre-Built PCs in Shoe, Shark Cases

January 4, 2023
How Do Hackers Hack Phones and How Can I Prevent It?

How Do Hackers Hack Phones and How Can I Prevent It?

September 30, 2022

Trending.

Review: Zoom ZPC-1

Review: Zoom ZPC-1

January 28, 2023
Image Creator now live in select countries for Microsoft Bing and coming soon in Microsoft Edge

Image Creator now live in select countries for Microsoft Bing and coming soon in Microsoft Edge

October 23, 2022
Elden Ring best spells 1.08: Tier lists, sorceries, incantations, and locations

Elden Ring best spells 1.08: Tier lists, sorceries, incantations, and locations

January 14, 2023
Allen Parr’s false teaching examined. Why you should unfollow him.

Allen Parr’s false teaching examined. Why you should unfollow him.

September 24, 2022
How to View Ring Doorbell on a Roku TV

How to View Ring Doorbell on a Roku TV

December 20, 2022

Flyy Tech

Welcome to Flyy Tech The goal of Flyy Tech is to give you the absolute best news sources for any topic! Our topics are carefully curated and constantly updated as we know the web moves fast so we try to as well.

Follow Us

Categories

  • Apple
  • Applications
  • Audio
  • Camera
  • Computers
  • Cooking
  • Entertainment
  • Fitness
  • Gaming
  • Laptop
  • lifestyle
  • Literature
  • Microsoft
  • Music
  • Podcasts
  • Review
  • Security
  • Smartphone
  • Travel
  • Uncategorized
  • Vlogs

Site Links

  • Home
  • About Us
  • Contact Us
  • Disclaimer
  • Privacy Policy
  • Terms & Conditions

Recent News

CS:GO smashes player records as Counter-Strike 2 hype mounts

CS:GO smashes player records as Counter-Strike 2 hype mounts

March 26, 2023
The one Diablo 4 tip I wish I knew before I started playing

The one Diablo 4 tip I wish I knew before I started playing

March 26, 2023

Copyright © 2022 Flyytech.com | All Rights Reserved.

No Result
View All Result
  • Home
  • Apple
  • Applications
    • Computers
    • Laptop
    • Microsoft
  • Security
  • Smartphone
  • Gaming
  • Entertainment
    • Literature
    • Cooking
    • Fitness
    • lifestyle
    • Music
    • Nature
    • Podcasts
    • Travel
    • Vlogs

Copyright © 2022 Flyytech.com | All Rights Reserved.

What Are Cookies
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Cookie SettingsAccept All
Manage consent

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
CookieDurationDescription
cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
Functional
Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
Performance
Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
Analytics
Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
Advertisement
Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
Others
Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
SAVE & ACCEPT