Skip to content

CenkCamkiran/DotnetCore-GRPC-First-Example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

First Example of Dotnet Core 6 GRPC (Client And Server)

Abstract

I was curious about GRPC. So I developed simple GRPC Client and Server projects via .NET Core 6.

Philosophy

Main goal is: Develop a API that has GRPC. Stream large files for example excel files etc. In this project I used excel file.

Contents

Features

  • Developed via .Net Core 6
  • Uses GRPC for streaming large files for example excel file etc.

Requirements

Note
Currently I use Docker version 20.10.21 and Docker Compose version v2.6.0
Currently I use Linux Ubuntu 18.06 LTS machine on Google Cloud.

Docker installation

Docker Engine and Docker Compose must be installed. Check out on Docker's offical site.

Excel File to Stream Large Data

Services (API Endpoints)

GetSalesData Method

Parameters

Name Required Type Description
n/a n/a n/a
n/a

Response

Content of Excel file will be streamed to client. You can see from console.

Note
❗ ❗ ❗ I used deadline as 5 seconds from Now. You can remove it or change it to whatever you want ❗ ❗ ❗

Business Logic

I didn't use any design patterns. This project is for learning about GRPC.

GRPC Client

  1. Connect to GRPC Server.
  2. Invoke GetSalesData Method without filter. I used deadline but you can change or remove it.
  3. Read messages until all the messages are fetched. Actually read excel file line by line (one row at a time) via GRPC Stream and print on console.
  4. If stream ended print total number of records from excel file that streamed via GRPC.
  5. If deadline or exception occurs print exception message

GRPC Server

  1. Read csv file named "sales_records.csv" line by line via loop.
  2. Split row data using comma ",". Seperator is comma in excel file.
  3. Write data into model named "SalesDataModel". This model is autogenerated from proto file named "sales.proto"
  4. Return model as a stream by simply writing our data model to the responseStream.
  5. If exception occurs return exception.

Proto File

I used this proto file.

syntax = "proto3";
import "google/protobuf/timestamp.proto";

option csharp_namespace = "Protos";

package sales;

service SalesService { //Service name
    rpc GetSalesData(Request) returns (stream SalesDataModel) {} //Method name is GetSalesData, takes type of Request object, returns SalesDataModel object as stream.  
}

message Request{
    string filters=1;
}

message SalesDataModel { //Excel file model

    int32 OrderID = 1;
    string Region = 2;
    string Country = 3;
    string ItemType=4;
    google.protobuf.Timestamp OrderDate=5;
    google.protobuf.Timestamp ShipDate=6;
    int32 UnitsSold=7;
    float UnitCost=8;
    float UnitPrice=9;
    int32 TotalRevenue=10;
    int32 TotalCost=11;
    int32 TotalProfit=12;

}

Installation

  • Use docker files to build docker images.
  • Use docker-compose files to run containers.

Example Commands to build images:

docker build -t cenkcamkiran/gcloud-projects:grpcexample_v1.0 . #build to use grpc server

docker build -t cenkcamkiran/gcloud-projects:grpcclient_v1.0 . #build to use grpc client

Contributing

I am open every advice for my project. I am planning to improve myself on .NET Core 6, Microservices and Container Technologies. So don't hesitate comment on my project. Every idea is plus for me.

Bug Reports & Feature Requests

Please use the Github issues.

RoadMap

I want to learn more details about GRPC.

About

First Example of Dotnet Core 6 GRPC (Client And Server)

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published