In our previous article (GRPC Service Create Using .NET Core 6.0 Entity Framework For CRUD Operation) we have seen how to create the gRPC service.
In this article, we are going to see how to consume the GRPC service at the client.
Note that these packages are applicable for any type of gRPC Client.
- Install-Package Grpc.Tools
- Install-Package Grpc.Net.Client
- Install-Package Google.Protobuf
In a different solution add a new .NET Core Console application.
Name this application as GRPCConsumeClient.
In this project add a new folder and name this as Proto.
Copy the products.proto file from the gRPC service project in this Client Protos folder and did the below setting changes
Modify the project file of the client project to configure the products.proto file to generate the client-side code.
<ItemGroup>
<FolderInclude="Proto\products.proto"GrpcServices="Client" />
</ItemGroup>
Change the namespace for the products.proto.
Right-Click on the products.proto file and select properties and make sure that Build Action is set to you Protobuf compiler and gRPC Stub Classes is set to Client only as shown
Add the below code In Program.cs for the data operation called from grpcservice
using Grpc.Net.Client;
using GRPCConsumeClient;
using System;
using System.Text.Json;
using static GRPCConsumeClient.ProductsService;
namespace CS_Console_CLients {
class Program {
static void Main(string[] args) {
Console.WriteLine("Press any key ");
Console.ReadLine();
var channel = GrpcChannel.ForAddress("http://localhost:5170");
var client = new ProductsServiceClient(channel);
GetAll(client);
Insert(client);
Console.WriteLine();
GetAll(client);
Console.ReadLine();
}
static void GetAll(ProductsServiceClient client) {
var products = client.GetAll(new Empty());
foreach(var item in products.Items) {
Console.WriteLine($ "{item.ProductRowId} { item.ProductId} { item.ProductName} { item.CategoryName} { item.Manufacturer} { item.Price} ");
}
}
static void Insert(ProductsServiceClient client) {
var product = client.Post(new Product() {
ProductId = "Prd-003",
ProductName = "Iron",
CategoryName = "Electrical",
Manufacturer = "MS-ECT",
Price = 3400
});
var product1 = client.Post(new Product() {
ProductId = "Prd-001",
ProductName = "Duster",
CategoryName = "Vehicle",
Manufacturer = "Renault",
Price = 1200000
});
var product2 = client.Post(new Product() {
ProductId = "Prd-002",
ProductName = "Baleno",
CategoryName = "Vehicle",
Manufacturer = "Maruti",
Price = 800000
});
Console.WriteLine($ "Record Added { JsonSerializer.Serialize(product)} ");
}
}
}
So our main extracting or calling the grpc service is,
var channel =
GrpcChannel.ForAddress("http://localhost:5170");
var client = new ProductsServiceClient(channel);
Now run the GrpcService exe as a separate server and run GrpcConsumeClient separate for the grpc service consuming.
Like below server and client are running. In the client we are seeing that our predefined data has been inserted and displaying.
We can use the grpcservice in our blazor UI also for the data operation
Source code
For GRPC service
For GRPC consume client
So now we have learned how to create a GRPC service and how to use that protobuf service in client application