Grpc With .Net Core by Mangesh Gaherwar || Lightup Conference
3K views
Nov 16, 2023
Session will focus on grpc anman getting started with it using .net core it will explain the basics of grpc why an how we can implement it using .net core Conference Website: https://www.2020twenty.net/lightup #lightup #2020twenty
View Video Transcript
0:56
We support this cause and
1:02
Help out the needy people. There. So this event wouldn't be possible
1:10
without these sponsors and we have plenty of sponsors. So thank you sponsors for the for for sponsoring
1:18
us this event and. Let's start the session so with with dotnet core 3.0 we have some exciting
1:27
We had some exciting upgrades. We have some exciting features and gRPC is one of them
1:34
so we'll try to explore what is gRPC. What are the basic concept of gRPC
1:39
How to create a service? How to create a client? And let's see how we can
1:45
We can call the call gRPC service using and let's let's create one demo app with
1:54
Blazor Web assembly as well as using some gRPC service as well
2:00
OK, so agenda for this session will be to to understand what is gRPC
2:05
Let's try to explore some gRPC concepts like protobuf how the things will how the client and
2:13
server communication occurs with the with gRPC. Then we will have one demo for creating a client and service
2:21
We will explore some gRPC types and then finally it would be a demo to do application with Blazor Web
2:29
Assembly and gRPC. OK, so. First thing, what is gRPC? So. Oh
2:51
I'm starting my video and. Yeah, so what is GRPC? Basically GRPC is open source RPC framework
3:01
which is been developed by Google. Actually Google uses Google Google has open sourced this in 2015
3:12
They Google has used this RPC framework into their internal for their
3:19
internal communication. Basically with the gRPC we can efficiently connect polyglot services
3:27
Big polyglot services are the services which are developed into two different languages or two
3:34
different completely environment environments in microservices architecture connecting mobile devices and some browser clients as well
3:42
It provides a way to it provides a way to the client app apps to call the server method as
3:49
local objects. This this this is basically a feature of the any
3:55
RPC any RPC framework. We can say that we use the client app or
4:01
client apps don't go into the details of what will be the
4:04
protocol or what will be? What will be the methods to call some
4:08
methods on onto the server like we use we do while while calling
4:12
the web API methods it would be simple. Simply it would be like
4:16
like calling the class method into into your like like like we do into our day to day development so
4:25
Next thing is to explore the gRPC core concept. Basically gRPC has two main parts. First one being the gRPC protocol that
4:36
is HTTP two and another one that is the data serialization form or data serialization method which which which is
4:46
nothing but which is nothing but the or we call it as an
4:49
protobuf protobuf either data is a data serialization format as well as the ideal which is ideal that is interface definition
4:59
language which is been used by gRPC so so as to communicate in
5:04
between your client and in between your service. So so these are the two main building block of of our gRPC service
5:13
So HTTP 2 it is an extension or our traditional HTTP, which provides some enormous features like performance or
5:24
streaming client side streaming, server side streaming, or as well as the bidirectional streaming
5:30
So these are so this is the main. So this is the main component of JRPG as well as the protobuf
5:39
So let's try to explore what is protobuf. Protobuf, which is also called as an protocol buffer
5:47
which is a flexible, efficient and structured mechanism for serializing the data
5:54
So in the same way that we use JSON into our web APIs or XML
6:00
we used to do XML like JSON or XML. This is the this is the mechanism to see for serializing
6:09
the data. Again, I just uh, coin one term that is ideal
6:15
It is used as an ideal ideal is nothing but an interface definition language
6:23
Generally, what is ideal ideal is ideal is the term which we used to or or it's it's
6:33
it's a mechanism to let a program written in one language to communicate
6:40
With another language like suppose my service is into a service has been returned into the C sharp then
6:50
There there my clients could be into the Android or into into
6:53
the job or it can be a Python Python client so protocol buffer
6:58
acts as an ideal so that and it it it means what it it allows
7:03
us or it it creates a platform so that these things these things
7:09
these things will be able to communicate with each other properly or without any hustle
7:16
This protocol buffer they are also used for service interface and message definitions as well So we see how what are the service interfaces and the message definitions are and these can be used to generate the source code for your choice of language using one compiler open source compiler is there which has been called as an proto see
7:41
So while demo we can see how to use the proto see or how we can generate the source code from this protobuf file
7:51
So. While dissecting the protobuf file, we can see. I'll just stick to the C-sharp protobuf file
8:05
So the first section is it tells us what is the syntax that is been used to generate the protobuf file
8:12
It is basically proto 3. So there are plenty of versions. There are like proto 2, proto 1, something like that
8:21
like that and currently we are using proto 3 and there is a C sharp namespace where all services
8:28
will be clubbed up that is the light up conference demo that I had created a demo next thing
8:36
is the service definition this is the sample service that we have created this service
8:43
this service is a greeter service. It contains the say hello method
8:51
and it accepts the hello request and returns the hello replies. Now, so to og with our web API things
9:03
this greeter is our controller and this say hello is our particular controller method
9:11
into our web API. And next these things like message hello request
9:15
and message hello reply are nothing but the models that we use into our web API
9:20
Basically what this service definition or what this protocol buffer file is telling us
9:25
is we are defining a greater service which have a say hello method
9:30
which accept a hello request object and it returns a hello reply
9:36
Okay, so let's. Let's try to dissect that more while while while I'll be I'll be doing one demo with you
9:47
OK, so basically how the communication between our grpc server and client occurs
9:56
So generally the grpc server implements a protobub service interface and runs a service
10:04
to handle call to its service methods. OK, generally what happens is at server end
10:11
gRPCs, at server end, the protocol buffer file provides some service interfaces
10:18
as well as some service methods which are implemented as a, which are implemented as a service
10:26
and which are implemented at the service end and at the gRPC server end
10:33
and it provides some service methods to be called by the client
10:38
On the other hand, this same protobuf file is used to create stubs
10:45
And this protobuf file can be distributed among any client, be it Android client
10:52
and it can be an Android client. We just give them our protofile
10:57
It will generate a stub. It will send a protorequest, and in return, it will get a protorespond
11:04
The same file can be distributed, distributed to another client that that can be a C++ client
11:12
Again, they will implement the stub from from our proto file again
11:18
They will send the proto request and they will get the proto response
11:23
So basically protobuf what protobuf allows us is it. It generates. Or we can develop a service into into
11:33
any language and we just distribute that particular protobuf to the different languages
11:39
To the different clients with the different languages basically. It can be an Android
11:47
client, it can be a C++ client or it can be a Python client or it can be a node client
11:53
So basically this is how the communication in between gRPC server and client occurs
12:00
So let's see how we can create our service and we can create a
12:08
We can create a client and how we can call the service from this client
12:15
So I'll just switch myself to my visual studio. I have one application already running over here
12:27
But what we will do is. We'll just add one new project
12:37
So you can see here there are there are templates. I mean G
12:44
This is the gRPC template which we use to create a gRPC server so when we click next
12:52
You will get the project name as usual. When you can you can you can
13:00
Light up service demos you can you can name it properly. Then this this box will open just
13:12
It will just ask us to create a. GRPC service. And here we go
13:19
Here our service. Is created so when we look at the folder structure and when we look at the the how the how the
13:29
how the things are arranged, you will see this protos file that is grid.proto
13:37
So basically this is the proto file and this is the service implementation
13:44
of that particular proto file. So again, like I have explained, this prototype uses the syntax
13:53
which is provided by proto3. This is our C sharp namespace. This is package which is being used internally
14:00
while creating a while generating a service interface for this. And this is our service definition
14:09
This is the messages and these two are these two are message types
14:16
These two are message types which have been which will be used into the service end
14:23
Okay, so. What we'll do is we'll just add one new service
14:29
and we'll see how we can consume it at the client's end
14:34
So I have one, I have created one. I have created, I have one demo already created
14:43
so we'll use that project to continue in this demo. So to add the protofile, just use the add new item option
14:54
item option Yes Click here proto file and let see it Let say it is
15:05
Hello world. Let's try to. Let's try to keep it as per the naming convention that proto
15:19
file name should be in lower case. Not sure why it is, but if you can, if you see this hello world dot proto file is been added
15:30
Now next thing that we need to do is we need to make sure that this proto file will be compiled
15:39
So in order to do that, this build action. Just go to the proto file
15:48
go to the protofile right click on it just click on the properties when we see the build action
15:53
here you can see the various options this is the protobuf compiler there is one action that is
16:01
which tells that this file should be compiled with this protobuf compiler and here we are saying
16:10
there are various options that how this file should be compiled as an client and server as a client
16:16
only as a server only or just don't do anything. So in this case, what we are going to do is we are going to compile this file as an server only so
16:30
With with. When we are done with these options, let's build the project once
16:42
The project is building might be my services are running. Into the background, so let me stop them
16:50
Yes. So it is stopped now I can build my projects. Yeah, here it is. We are successful with that now to define the service. The first step that we need to do is to define our according to me. I mean you can take whatever the approach you want. You can define the service first and then define your messages
17:14
but what will be the convenient way is to define our messages
17:20
Okay, so message my messages will be. Hello request. Which will return
17:35
Hello response. In this. Hello request, I will just say. I'll just create one
18:14
One so one thing interesting thing that that we can we can check here is
18:22
This this every field into this message had some numbering with it
18:28
That is one what what this number numbering is. So basically these numbers indicate the fields number which are used to identify fields in the binary encoded data
18:42
basically protocol protobuf encodes the data into the while sending into the wire
18:49
It is binary encoded. So these numbers are used to recognize that particular field
18:55
These numbers are used to recognize that particular field. So when we are done with this
19:05
so when we are done with this, then it's time to create
20:15
okay
20:33
i guess i misplaced it somewhere so let me add one proto file again
20:45
you okay oh it was there actually need to include this into the project
21:15
I'm not sure why it is not showing into this but and
21:45
So let's let's try to generate this service and into this service
21:55
What we'll do is we'll just say that RPC. Say hello. Which will accept this hello request and which will returns us the hello response. Comma
22:12
And let's try to build this again. So yes, this is getting build
22:21
Now. What we'll do is. We will add So This all this all has become our contract This this all has become our contract Now we are going to implement this contract into
22:39
Into the service. Hello world. Service
22:57
Now we have added this hello world service. Next step is. Make sure whatever
23:22
sure why it is let me do it again
23:37
i can do it something like this and
24:19
Yeah, here we can see. Dot hello base. So this is the C sharp our C sharp namespace
24:31
We can we can we can make it into this using statement maybe
24:39
And now. We know this is the hello world where it is
24:46
This is the now we can see this. Hello world is the is our service
24:52
definition and this is the hello world base. This is nothing but an abstract class
25:01
which implements all all the things and binds the binds the service call
25:08
to this proto proto file basically. So that that is the that that is
25:13
been achieved by using the service base. Now to use this method into this proto file
25:21
what we need to do is we need to. Override. This method into this service implementation
25:36
and what we will do is we will just return. So
26:11
Here we have implemented our service in which this this is the simple service which is which
26:18
is returning our hello response. It contains one property that is greeting
26:22
We are just appending some hello and whatever the request name that we will be sending from
26:27
the client it would be it would send us as an hello that particular client
26:32
OK or hello that particular name. So now next step is to generate the clients
26:41
So for that I have added one console application and this in this we will use to call this
26:49
Oh, before that, what we need to do is. Uh, before that, what we need to do is we just need
26:57
to map this service or map this endpoint into our. This middleware so to to to do that
27:06
There is one. Well, where for that? Just need to do this again
27:20
If you could see. If we try to access these services from the browser
27:26
then it will give us this message that communication with gRPC endpoints cannot be
27:32
Made it can be made only through a gRPC clients so in order to invoke
27:38
these services we need to have a gRPC client which will implement
27:43
these services or these protocol buffer files so now next step is
27:49
to create a client so how do we create a client we just need to do
27:54
is just copy this proto file. Spaced. In your protos folder or or
28:01
or whatever location you want and just go to the properties of it and just change the
28:11
step classes like how the step classes will be generated we want it to be generated as a client
28:19
only just apply this and click ok now this file or this service is ready to be consumed into our
28:29
client application so before that in order to make any client or i'm talking specifically about
28:38
the dot net client we need some three packages before that first one being google dot protobuf
28:45
second one is the grpc dot net dot client and third one is the grpc dot tools so these these things
28:53
are needed. So these these things will provide us the things like protocol proto C or or the orientation for the protobuf
29:03
files so. Now we have we have this proto file we have made it to compile
29:12
as an client. Now it's time to consume this file. And see how it will work
29:19
So I'll just close all of them. I will just go to the program dot CS
29:28
And. This is my older demo basically. Let's keep it. So first step into into this is to create a client so create a channel
29:46
So what channel does is it just creates a connection in between
29:51
your client and in between your server. So we are assuming that our services are running on this
29:59
That is 5001 so. To do that just just just do one thing
30:08
I'm using this dot net run. OK, there are some issues or runtime issues
30:22
Let me see what it is. Oh
30:40
There are some issues with this. OK. This endpoint will not be there because we have
30:50
commented that and let's try to run this again. uh yeah we have this also let's comment this all time and let's try to run this again
31:21
I mean. OK, yeah, our server is running with this and now
31:36
Let's go to our client. Let's let's build up our client first. Then this is how we create a channel
31:46
Now. Once we create a channel next next step is to create a
31:52
client, so the client is nothing but. This hello world that we have
32:07
And. We need to provide a connection for that, OK? So this client
32:16
is nothing but. This client is nothing but a this this client is nothing but a
32:25
client generated by our protobuf file. So when we when you go and when you
32:31
see into this so you will see that this all the class is been generated
32:36
from a protobuf file when we compiled it. So this is this is our client now
32:42
To get a. To get to get our or just to call a method from our server
32:56
What we need to do is. We just need to. Just come in this and
33:05
client dot say hello it accept
33:14
new hello request which has name we will call we will give it as an
33:24
abc for now and that's it so we'll add some for here and
33:35
Yeah, we're ready to go. And we can see this response has
33:42
When property called greetings and which will will be printing on the console app so maybe
33:52
So yeah, our service is running over here. Now let's try to run this this client and see what happens over here
34:01
So yeah, I will say this as my as my startup project and yeah
34:08
Here I'm running it. OK so. Yeah, now now you can see here the response from the services
34:22
Hello, ABC in the same way that we have implemented. With this hour
34:27
Hello world service it. What it is returning is it is returning the greetings with
34:33
this hello and whatever the name that we are passing. To this client from here
34:41
Here you can say if I change this to maybe. Maybe to Tony Stark
34:55
Then let's see what it will return. Yeah it is returning now Hello Tony Stark and on and at the server server end if you could see that this request is been finished
35:11
within this. Within this timeframe with status code 200 and with the content type as an application slash GRPC
35:22
So this is how we can create our services and this is how we can consume them
35:28
into our client application so. So coming coming back to. So coming back to our
35:39
They more power PPD. So a. So GRPC types. So basically there are four types of GRPC types
35:50
First one being the unary that is a simple request and response
35:55
format or the way we send the request server process the required request and it sends us back the responses. That is we
36:04
call it as an unary. Next one is the client streaming. What
36:09
happens in this case is client streams the request servers gets them, accept them all, process them and sends us the
36:20
response at once. Next one is the server streaming. The client the client initiates the request in return
36:28
Server holds that holds that request on on behalf of that request
36:32
It sends the stream of the responses to the client and client can act on that or client
36:39
can operate on that that that on that particular stream of the responses
36:44
Next one is the bi-directional streaming means what happens is client can send the streams
36:51
in response server also can send us the stream data. So these are the four types of the GRPCs, right
37:01
Next, we have one demo that demo, I have one ready-made demo with me
37:08
which is nothing but the integration of our WebAssembly and our GRPC service
37:16
Basically, there is one limitation with the GRPC service. What would that limitation is we cannot call our grpc service from the browser like I had
37:26
I had shown you that message while into the service definition So in in order to avoid that there is one package or there is one another form of grpc
37:37
That we call as an grpc web that this limitation is because of some some
37:43
HTTP to Compliances with the browser as well as browser is not ready to accept some
37:52
Or browser is not ready to Adopt with with some some some something some important things with the HTTP suit
38:01
So that's why the gRPC service cannot be directly consumed into the browser
38:06
So in order to you know in order to Overcome that there is one there is one way out for that that is gRPC web
38:15
So this application is basically developed into the Blazor WebAssembly and. Into the into the service side we are using
38:26
gRPC web and gRPC so as to implement the services. So let's again go back to the Visual Studio
38:39
And this is my. Ready readymade app. Now let me stop. This service service station because my service will be
38:52
running on the on the same port and yeah, this is shutting down
38:59
OK, so let me go back to my source code again and
39:04
Yeah, so this is my service application. This is a to do
39:09
application. Basically it has my to do data service. What it is taking is it it will it will just get us all the list
39:19
It will post or it will be to add the database to add the to do item into the database
39:25
This will get a particular data item to do data item deleted and updated
39:33
So I'm just you don't need to make it like put delete or get something like that
39:40
We don't need to use that terms. You can use whatever the terms you want to like post
39:46
I just made it to look like something like web APIs. OK, so
39:52
In this in this in this application I'm using some in memory database to to to try it out and this is my to do
40:01
service and these are the initializations Now And this is my WebAssembly app so I just start my to do applications once
40:19
Open power shell window open over here and I'll just click.net run
40:24
Yes, so now my service is running on this localhost 5001
40:36
Now it's time to start our. Client application, so this client application is completely built up on the Blazor web assembly
40:49
So in the same way, like we have developed the client, we have
40:54
The to do dot proto client it has all the methods or it has
41:00
all the methods, all the message definitions, imports. Syntax and I'm just making it to compile
41:09
As an client only. So when I start this application. It is starting
41:24
It is loading actually, yeah, so this is this is my this is my
41:31
seeded data that I had done while running the application and yeah, now you can see into into into the services
41:41
Uh, not here. Yeah, here. Uh
41:51
This this service is running. So let's. Now try to what what we'll try to do is just add one new item
42:02
Just say meetings. At. 10 AM tomorrow status is active. All good
42:11
OK, now maybe we can have some refinements. At 11am and the status is active
42:22
Good. Again, I can delete this. All good. I can edit this
42:30
Just make it closed that my particular to do item is done
42:35
So this is this is the application that that we have developed into the Blazor Web assembly and with
42:43
with gRPC web and gRPC services. If you if you. If you just yze
42:51
Some network calls with this like test and test and. Active. Here you can see
43:02
That this call. OK
43:43
traditional web API. So this is this is how we can. I mean this
43:47
this is how we can develop or we can use. gRPC web to to to just make sure that we could to build up the
43:57
browser apps into the web assembly or into the angular or in in in any other technologies also. So this is my blazer
44:07
This is my. This is my to do application now. Coming to this
44:34
So, some startup setups that we need to make before starting using jrpcweb
44:49
So this is adding some cores like we do into the web API but with the headers like jrpc
44:56
status, jrpc message, jrpc encoding and accept encoding. Next is to add services like services.addjrpc
45:05
This is usual. Next is to add or to configure a middleware into the pipeline to tell that we are going to use the gRPC web and to make sure that these endpoints just enabled with the course that that we have developed
45:27
or that we have that we have mentioned into this services services setting so this is how we can
45:34
can develop develop the. This is how we can integrate our web
45:40
assembly apps with our gRPC service. I mean, yeah. So coming back to our presentation. So
45:54
Just just just just few questions that there might be like why we
45:59
should use the gRPC instead of web API. Actually, yes, that's the most debatable point
46:07
It would develop debatable point like why we should. We should switch over to the gRPC if we have the way
46:14
API like excellent framework like web API. First thing or excellent framework like web API
46:20
where we are so used to it. We can develop the things quickly and why
46:25
why we should use the gRPC. First thing is the performance. With the HTTP to
46:32
The performance thing and the gRPC uses HTTP2 and with with HTTP2 there are some moderate
46:41
improvements or there are some considerable improvements into the performance of the application
46:48
or how the communication will be occur. Next is the code generation
46:53
Like the code generation is a little bit easy when we are using the protobuf
47:01
And means like we used to do for the WSDL into the WCF era that will just generate some
47:11
clients or stubs and we used to do that. In the same way, we can do that for the protobuf as well
47:18
But we cannot do that in case of Web APIs. Next is the strict specification
47:25
Though generally there is no formal specification for the rest. There is no like what verbs to be used for a particular method or what what what should
47:35
be the return types? What should be the output or how the output should be structured
47:40
So there is no formal specification is been defined with the rest, but with the GRPC with
47:46
the protobuf with the with the with with the protobuf we have some strict specification
47:53
which is been defined. Next is the streaming support. We have some we have streaming support like clients
48:00
client side streaming, server side streaming and bi-directional streaming, which is not possible with the traditional rest
48:08
This is well suited for the microservices for polyglot environments and again. Being
48:17
The less what we can say being lightweight than the rest response. This can be the alternative to the rest into the
48:29
network constraint areas. So for polyglot environment, yes, whenever there are two
48:37
different languages involved, then there are always issues. So JRP C or protobuf JRP C with protobuf can be the can can
48:48
be helpful in in that case into the inter services communication to use to use into the microservices
48:57
We can use the gRPC. So these are these are the. These are the plus points of the gRPC
49:04
but there are some negative points as well. Like there is no directly support
49:09
to the direct support on the browser. As far as the gRPC is concerned
49:15
so that that can make so that that is. That is something that that gRPC
49:22
needs to be matured on. So these are there are there are there are plenty of limitations
49:28
with the GR pieces as well, so. So any questions with this
49:36
I guess no. So yeah, thank you. And this is these are my contact details. You can email
49:46
me. On my email ID that one gauge dot care were at the
49:51
You can find me on LinkedIn as well. My GitHub ID is dotnet geek
49:55
All these samples that is been into the into the into the demo
50:00
are present on this GitHub GitHub repository and you can find me on the Twitter as well
50:06
I tweet on the name under the name of dotnet geek one and I blog at www.netgeek1
#Programming
#Windows & .NET