gRPC - High performance API communications with .NET

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi everyone today we're going to talk about grpc a high performance open source remote procedural framework that will let you make highly efficient straightforward and lightweight requests to your server it has some caveats that we will explore but it's great for some specific use cases and being used by companies like netflix so let's go to the code and see how we can implement this framework using.net okay here i have my workspace let me first explain you what we're going to need we require the.net 6 sdk to make our.net 6 application of course and i would recommend you to use something called bloom rpc this is an application like postman but to test protofiles which is like the signature file that we use on grpc that has the whole definition of our service or our api we will use it to test are the results of our service yeah i would recommend you to have it but you can use any grpc client that you find on the internet in this case i will be using bloom rpc okay to begin let's make a the from the command.net new new grpc this will create our scaffolding default grpc project it's created let me restore the dependencies in the jrbc project you will always have the grpc asp.net core dependency and this will help you generate your profiles and everything so let's see first our profile this is the the most important part in of grpc and what makes it agnostic like you can have the same profile in multiple languages and it will generate the same assets based on the language technically what happens is that based on this profile which is like a contract a dotnet is generating a lot of files that are using that we can use as classes in here as you can see here i have a method rpc say hello it's a method we need to send it the hello request and it will return a hello reply here we have the definition of of what it's a hello request it has a string name and the the this the number of this property the it will be the first property it's based on the order so the reply it's almost the same it returns a message is a string and it's the first position in the message so once we have this uh i don't remember if it's building like we don't care when it happens but this will be converted as classes by net and then we can use them on our greeter service so here we have the hello reply hello request the say hello method and we just returned that the same way this is your pc so we have a specific code methods imagine that you want to create a method called the to create a user you literally create a method called create user that receives the uh a user creation request and sends a user creation reply and that should make it that's the basics of our pc it's not that modern we have seen that in a lot of technologies before and you might be thinking yeah i would prefer to use rest and you're completely right it's it's absolutely a good idea to use rest but imagine that you need a really high performance task it will be great for you to use grpc because it takes a advantage the advantages of http 2 instead of http 1.1 which is only text based transfers you know you can do binary transfers using the rpc which will lead to even eight times faster requests than you would that done the things that you can do using rest and it's pretty lightweight it's not super lightweight like it can be 30 percent less weight less weight than a jason request using rest but yeah it's an advantage in any way um but yes and it's also this the this definition it's also as i said language agnostic you can copy your profile to another language and maybe create an api in python java or whatever it's it's a great solution for that type of stuff it can also lead to some issues because if you think about it with the profile you whenever whoever has your profile can leak your information because it will have the the whole definition of your service but it's i think it's a but kind of small caveat because if you have to think that if someone gets your open api for your rest api they can also do the same thing so yeah but okay and now that we understand this example and we we know that the profile it's a signature of our api the definition of how we someone can call it and how our data structures and methods are defined then um we now that we know that we can explore also a greater service which is exactly this definition i have service gridder with a method greater service with a method greater base this is a this is extending the classes generated from from the profile i think if we go to the bim folder or somewhere let me build this project terminal if we build maybe even without building i'm not sure where everything is getting stored yeah it's it's somewhere let me let me just go here and just take the hello replay and go to the implementation of it so as you can see here on my on my build on my object i have the grid definition the grid proto definition so here it's a lot generated ugly code that comes from your pc and yeah it basically creates the classes and the data structures that i need to fulfill this uh contract that i created so okay now that we know that let's create an api literally i came here having no idea of what specifically to do so let's create a contacts api to save contacts it's easy it's cool it's fantastic and we'll be quick so yeah and let's create a contact service contact the services contact and i'm going to have a method called add contact then we need to define and add contacts contact request add contact request which return and add contact or maybe let's define a contact response response it would help a lot it would be really really rest like if we just keep one response but yeah let's forget it i think it's useful and it's okay so let's have our ad contact request the name property works fine well let's change it to first name the definition normally is the standard to use the the underscore to separate words instead of camel case or pascal case in this case uh so i will keep with doing that it's it's the standard that you can see on google projects and some and stuff like that so let's keep going that way last name too and the email i think that that's enough i want to add a boolean just to show you another type it's active and i think that that should be okay uh normally you have the basic types you can also use something called stream if you want to return like array of arrays of stuff or well to keep a stream open where you can send more information yeah there are multiple ways to manage this and you have a lot of types you can check the documentation of our piece of grpc to know more specific things about the types there also a specific libraries that you can install to have like a like i can say company specific types like one from google and others but okay i have my add contact request with which requires me to send a first name last name email and if the if the contact will be active or not uh we also need to define our contact repo response you might see that some erpc projects have the reply for the response classes in my case i will use their response name i think it's it's a better convention and it's something that anyone that has worked with services before probably with on would understand so yeah i think i'm going to return something exactly the same um but i'm going to add a message abilene that it's success and why not a text message which is a string a mess result message should be my sixth thing so let me delete all these comments i think we don't need them anymore because i'm here for you to explain everything um [Music] let's change the package name i think this package name doesn't represent anything to my address book hey let's call it yes address that's it oh address book yeah not respect why not okay now that we have our address book proto definition let's do an overview of what we have here so to make sure that we understand everything we have the syntax of our profile which is basically the standard that you're going to use to generate our your profile a lot of companies have their own standard you will find a google product blah blah blah a lot of stuff in this case we're using pro the basic one so yeah and we need we have an option called c sharp namespace it's a dot net specific thing so we know that every generated artifact on the proto of the proto will be on the grpc namespace it's really useful and we need it when we have to use our classes and all that stuff that's generated on our service we will see that later so we have our package as you can see i named my profile the same way as my package that's the standard the way that it should work we have a service definition we with the service is called contacts and i have a method called that contact it re receives an ad contract request and returns a contract response i have my my my data structure definitions my ad contact request with a with a with the property that i require and my contact repo response with response response with my with my output with a while i'm going to go to to turn back to the to a requester so yes and that's it as you can see every single parameter well property has uh a value which represent the order of reception of the of every single item in the in the object and that's it once we have our profile we need to include it in our project how do we do that okay we go to our cs profile add an item group and then protobooth include produce addressbook.pro and then we need to define who is using this profile why we need to define this it could be two if you have if you're doing a client app you need to set this as client if you're doing a server app a server or if you want if your your application do does both you're generating and uh like you're doing the server and the client at the same time which is completely possible you can leave it without this attribute or just set it as both and it should work basically this limits the while it's being generated from the pro from the profile because like you don't need the service base and all that stuff in case that you're doing a client app so those will not be generated if you're doing a client application in this case because i'm doing a server application that i'm going to consume using bloom rpc i'm going to leave it a server so yeah it takes my my profile and that would generate a let's see maybe i have an issue contact okay i have an issue on my uh profile as you can see if the omnisharp it's notifying me that i'm getting an issue and that's because of this i changed the name but i didn't change it there and now it should build and will generate my assets if i go here in products i see that here i have my auto generated assets that i can consume on my code layer so now that i have that and i have already find my product i have added to my cs proc file it's time to define our service logic i already have one here so i import my grpc namespace which is the one that i specified here on my on my profile and your pc core to use some your pc stuff so while the only thing that we're getting actually from the jrpc core is the server server call contacts that we could use to get some request parameters or specific information from the requester and from the server it's a it's a it's like an http context but no like the web context yeah the web context that we have always used on controllers so yeah so what happens here is that i have to create a class called contact service and it will extend from grpc contact contact base this comes from the service definition i have contact and this creates a class called contact base that i can use on my service to override methods this will make sure that i have the exact same signature as expected from my profile so yeah i extend from that so i can override its methods like the add contact method that i have here let me change this to contact response and put it here i will have an issue because i think it's ha it's haven't generated anything let me save and also maybe make a change here or something if not i'm going to make a net build it should be automatic in real life okay a contact response contact response and yeah now it's generated cool and now it's generated and everything as you can see well i have my method i need to override it so my pro definition says that i return a contact response and because this is an async method public async method well i shouldn't make it a sync because i don't have anything as sync inside so it's a lie well that's where i think but it's not sync yeah so well anyway yeah it's nothing let's just return the task but okay i have here my task that returns the contact response the method is called add contacts and i receive the a classical add context contact request it's a request and i have my server called contacts which is like my web context and i create a new contact based on my request here i have a contact class this is my model and i created it on base of what we have on my profile it's the same thing uh i just receive and add context request contact request in the constructor and it will be compared to a contact that i can add to a list or a database in this case i don't have a real persistence i just have a list that i'm not really sure if it's going to stay for the next request but yeah it serves for the purpose of the example so yeah i take my contact and i add it to the list once that's done i return my result which is the same data from the request i said that it's active i'm going to send this from the request also request well i also yeah maybe take it from the contact would be the best thing i'm going to use the contact contact email contact contact contact and that makes it and i said that it's success because it's finished and and the contact was added successfully and yeah and that makes it but then we need to add that to our program to our minimal hosting thing similar to rest so for this we just need to set up a little thing app map grpc service contact service so that should be enough to have our little service running er our api that's context to uh list cool so now i have that and let me just clean this up just a bit to have it like really click here i don't need even the app get thing and this is just for i think for an http if someone does an actual request it will be returning this all the time so everyone will know that how to use the client yeah i'm going to like put it into potato that should be enough that looks cool potato so now they that we have that we need to trust the certificates uh to use it locally so we i'm going to run a little comment here which is dotnet dev search https trust that will make my pc trust the ca certificate of the local machine and let's run our application.net run and hope for the best i'm not sure if it's going to work i hope so okay it seems to be running uh http 1596 and 71.95 for a https so now let's open our bloom rpc to test our api so i already have here my profile imported i just click the plus button select my profile the same one that i'm using on my server and that will give me this uh definition it will read the definition and show me the methods the services and the packages that i have on my profile and here it shows a little sample with the parameters that i have on my pro definition it looks like a json but it's in real life it's not json this is this application just show it that way because it's easier to understand and again we can define metadata and a lot of stuff in this test drpc testing application so i already have my server here as you can see it's the same one that i have here and alcohol 1596 i say it up there and okay let's test it out so uh first i have my data pablo and that should be enough to save our data so if i click play it should work so yeah it was really quick actually less than much less than a second it's just 0.064 seconds at 10 0 7 7 7 7 7 8. as you can see it's local so basically this is just processing a power being reflected but it's really quick it's just we're just adding an elite so it's not like a real data but yes i think it works and okay i received the i received back my response and as you can see it's really easy it was really quick to implement and i don't have a lot of code for it i think we also need more definitions when we try when we try to define a rest api because we need to define like the methods uh if maybe if we need a queries a query string from from query a filter a lot of stuff like here we just have a definition that it's language agnostic and we can use it anywhere well i think that's seen that covers what we need to know about grpc how we can implement it using.net creating our product file and adding our service well that's your pc of course it's not replacement for rest but it's a really cool alternative for stuff like event drive and architecture client streaming server streaming and v-directional streaming and of course you can make power requests and everything thanks to http 2. if you don't need a request response implementation maybe grpc is your best is your best way to go even though it has its weaknesses it it some lack of maturity and a big learning curve but it's a really cool tool that maybe you should consider on your next design and don't forget that you can having the same api parallel implementations with rest and your pc on the other side and maybe for a specific channels that can take advantage of those features you can have that channel it's really cool so if you like this video don't forget to press the like button and subscribe if you like to see more content of us we're trying to keep you updated with some stuff that i i assure you you will learn something and also you can check our other videos those are great and i promise as i said before that you will learn something from us it's really cool to have you here and comment if you want to see something specific that's it from my side i really hope you have a fantastic day and happy coding you
Info
Channel: Hahn Software
Views: 223
Rating: undefined out of 5
Keywords: #C, TypeScript, Angular, .NET, dot net, API, gRPC
Id: DIqw85zBEU8
Channel Id: undefined
Length: 23min 31sec (1411 seconds)
Published: Fri Sep 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.