gRPC - Getting Started .NET 5/ .NET Core 3.x

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's up youtube welcome to this part two of grpc while the first part was more generalized or abstracted from dotnet c sharp this part is and the following parts are very specific to dotnet c job so today is the getting started and i'm sort of saying getting started in a way that it's not the simple greeter kind of service that you will see i'm going to be covering quite a few more details which i think are going to be important moving forward in the in the next parts and i'm going to be talking about some specific performance enhancements of grpc in dotnet but then as the parts go along maybe one or two more parts i can't imagine there's going to be many more than that i'm going to get to the point where we're going to be talking more real world production quality code and what to and how to do the stuff and where to write this code and things of that nature so today is the basic nonetheless the service is not basic it's not like the standard greeter service that you see and people talk about including the microsoft documentation there are things you're going to get your trip over if you don't if you just follow the greeter service and so this getting started is going to be more real world-ish and then and the following parts will be even more towards production code all right so starting with performance you should know if you don't know anything else grpc performance in dot net core and dot net framework five dot net five i guess is out of this world the performance that the team has been able to achieve as compared to other platforms like c c plus plus go java is phenomenal most of the benefits have actually come from the fact that we have in dotnetcore.netframework.net 5. a completely managed framework built in dot net five four dot net five while other languages some of them use the grpc c as in c c language core and others like java go and dart i'm stuck in the slide here have their own core that's built from their own language so the net 5 core grpc core and the net core 3.1 plus grpc core are built in fully managed code there are some pros and cons but mainly pros and fewer cons that even allow for this whole cross-platform idea and the ability to get that performance cross-platform so just want you to keep that in mind this slide is from the previous video if you haven't seen the previous video i'll put a link to it over here so just to reiterate here as you can see from the slide json versus protobuf for protocol buffers the size difference between a json serialized data packet going across the wire and a protobuf or protocol buffer packet going across a wire the protocol packet buffer or protobufs packet size the data going across the wire is a third to a quarter that of json and that's almost obvious there are some things to understand here i'm going to talk a little deeper just a little bit deeper as i'm showing you the dot proto files you understand one of the reasons why this file is so small vocal is binary serialized which is already going to be smaller all right so there's that difference the second part is that grpc the framework the dotnet core or dot net framework framework is 25 faster than the sort of mvc part of things right the web api mvc all of that that's not the reason you should be using it i'm just just the way it's been implemented makes it that much faster so it's all pre-compiled code whereas in web api and mvc they do things like at runtime to do the dispatching we call it to dispatch to the different controllers and actions and to hydrate the data here's serialized deserialized in grpc as binary but there's no it's already pre-known right it's all strongly typed and things like that so it's faster in that sense since grpc has this idea of bi-directional streaming if you don't know what that is please look at the video link to earlier it's not a fair fight if you will to compare web api to grpc but using jpc bi-directional streaming that's 140 faster than mvc web api okay so that's a huge difference there if your application requires that i'm not saying you should be moving to grpc for performance reasons certainly i do think it's the future whatever that means to you [Music] you it's almost like how xml kind of died of slow death i think json will eventually kind of die slow that just because it's just way too verbose and uh loosey-goosey right i mean you didn't you realize after you made a request that there's something wrong with the request and similarly when you get a response you have to after the response have to figure out if you've got the right data which is not necessarily the best way to go especially for internal applications internal to your organization services go the language golang and we call it a go it's grpc core is considered one of the fastest implementations this i think neck and neck would see if not faster don't really remember anyway the dotnet 5 grpc core that box that i showed you earlier is neck and neck with the core version of it in some cases exceeds the performance some cases slightly slower but net net they're both on on par and that's something to be said about not just because we are dominant programmers i think the team has done some phenomenal things some of these things are nothing to do with grpc they started when dot net core started when you know the split between dotnetframe.net core mainly the tcp stack and a lot and a lot of improvements have been made over the tcp stack and the pipeline which essentially deals with parsing so if you imagine the tcp stack and all the memory management that has to occur of receiving bytes and accumulating data and parsing that data that's the area where the biggest improvements have come in net core and of course in hp http 2 implementations and therefore grpc so what gives us these performance results is of course http 2 as i've explained in the previous video the all the benefits with http 2 binary framing multiplexing full duplex all that hdb compression header compression here we have http client that's the net http client it has seen some phenomenal improvements with regards to reduced latency in connections and allocations but it's also basically coming due to the http 2 implementation improvements and improving concurrency within the http client right the hpac compression is the if you watch the first video i said there's a it's not g zip or z lib it's h-pack so if you come here after that video it was hpac that was forgotten the name of hpac is another kind of compression it was essentially it came about because of some security issues with gzip and our g z lib or g zip compression and so h pack is the new compression it's nothing to do with protobuf or grpc itself but that's what grpc uses to compress the http headers in http 2. keep alive things allow the client and server to stay in touch and so we don't want to keep establishing the connection even though they are not really having a conversation if the client requires the connection to stay alive the client can send the key typing and there is server response the next step something that's ongoing or this work in progress is starting to include or use the span of t feature we introduce i think in dot net seven again this is a performance feature that was introduced in the language where we're allowed to essentially see a window within an array so rather than then when you're parsing to see a subset of an array and have an allocation occur because you need the subset the span allows you to just look into that data without creating a subset essentially the lack of or they need to not have to allocate while you're parsing byte arrays or strings is where the benefits are coming you know fewer allocations therefore fewer garbage collections and so they're looking at the the protobuf team the protocol buffers team not the dot net c sharp team is working with the dotnet team to see if they can include the span of t feature of dotnet in the parsing logic of the protobuf civilization feature so there's a two teams working together trying to get that improvement and once that happens we could probably get some more benefits in the whole performance deal there the previous video i mentioned that the transport layer is pluggable currently using tcp tomorrow it could be quick if you remember but other transports are also possible like as you can see on the slide named pipes and the unix domain sockets these are inter process communications mechanisms have been around for many many years where even before i started programming because i remember looking into that and using it in one of the first few programs that i built and so in windows we have name pipes on linux unix we have the unix domain sockets essentially these are inter process protocol meaning within the same machine if two processes want to talk to each other almost like tcps client server ish is two-way bi-directional you can use name pipes now sql server if you've installed sql server sql server and sql server express the local edition both have the feature or ability to use named pipes instead of tcp and if you want you can try and switch it over to using named pipes and of course your connection string from the dot net side will also have to change you will see a market improvement in performance because name pipes are literally just allocating a uh memory on the machine common area such that both parties can write to these pipes you know of memory so it's literally in memory for both parties there's no socket involved anyway so that's a feature that's i think already in place i haven't tried it yet i might you know before the end of the series if i get the chance i'll look into the name pipes feature and make a video on that but it's there it's a feature it's nice to have missed one slide just a quick note this is also important um a grpc requires http 2. so anything you do any platform you plan to use on the first thing you have to make sure is that that platform the web servers things of that nature support http too without which grpc is not going to function so as a result there are some limitations as you can see here only our net core 3x and net 5 support grpc on the operating systems side of things you require is if you're using iis then ios needs to be on this windows blah blah and your application needs to be dotnet 5. is also depends on the kernel mode driver called httpsys and that requires minimum windows bill of that these are not recent bills so it's not something that you might have to be worried about but nonetheless be aware that there are these limitations in azure the cloud services don't support grpc app services also i think only supports grpc web that's one of the things i'll try and get to in the future videos grpc web is different from grpc um i don't think azure has a really good story uh aks the kubernetes uh containers do support grpc but if you're not in kubernetes then you're not getting grpc which is unfortunate by this point i'm you know oh i was hoping that microsoft had that act together where multiple teams can work in parallel and get to an end point and then have it all available anyway now we move on to the project all right so i'm simply going to start select from this type of project it's called the asp.net core grpc project just plain jane we'll create it plain j and then we'll modify it so i'm just going to call it just let's call it movie grpc service if need be i'll also make this video the code basically available on my github and if i do then i'll put a link in the description below so if you are interested in the code of course remember this is night five if you are understand the code then look at the link sorry in the description if the link is there you can go get the code if i've forgotten the link please remind me okay so notice that start net5 as you can see this is standard asp.net core we have our startup we've got our program cst the code here is identical to what you would expect nothing special here all right it is a asp.net core application what they have done is they've included in the startup you can see here they've added grpc okay so if you want to use grpc you need to say services.add grpc and then make sure you do this after use routing and within the endpoints of these two are required to be in this order that then you have to do the use routing before you do the use endpoints anyways so i'm just kind of reminding you there within the endpoints you have to set up a mapping for your grp servers in this case it's called greater service so you need if you have multiple services just add multiple lines here to get the grpc service framework to start routing to or dispatching to your service if the requirement if the request is coming or headed for your service that's really what's doing this this feature is just saying hey i'm here as a grpc service and if you get any requests please hand them to me for me not for somebody else right this is a standard http endpoint in case you're using jrpc only then this is a basic reminder as you can see from the comment here if you're trying to access this service using the standard http then it's gonna give you this information error message right so that's the change and the service and the mappings if you have multiple sources and multiple mappings okay now let's look at some of the files that were generated here in the produce folder these are conventions that the project system follows and i suggest you follow the same in the produce folder we have a gritty greet dot photo file and the services folder we have a greater service all this is code gen for you if you remember from the previous video the protofile is our idl or the interface definition language that grpc uses to communicate the the service so i have a proto file for my service i publish that profile i hand it to your emaility whatever i do to it i can send it to you in some form of fashion make it available to you in some form of fashion even through url endpoint and then that describes for you the service i am publishing and then you can use visual studio and that profile to generate a client for my source right kind of like soap used to do with the wisdom or you do now swashbuckle open api all right all right now one of the things i want to mention here is that when this each time this protofile is modified the code generation functionality in visual studio will regenerate some classes that are auto generated for you that pertain to this dot profile the class i'm talking about are the classes that i generated here so these are this these classes are all code generated so we'll get to that i don't want to spend too much time with the greater part we're going to change this to another profile with different services and different inputs and outputs and then we can talk about that okay so let's do that so we have now a service called movie service that has one method called get movies that takes an argument and returns an argument this is standard for all grpc methods have to take exactly one argument and they have to return exactly one argument so in this case is taking nothing effectively this empty is a what we call a well-known type in grpc there are different types of scalar types that are documented as part of the protocol buffers thing and then there are well-known types that are not quite part of the normal they are like an extension if you will and google has defined a few well-known types the empty being one of them but there's also equivalence to our dot net types daytime offset timestamp and daytime all right so but they're not defaults in the protobufs but they're well known types and so when you use some other definition you can define your own tool you will import that here so we are importing this in this case google protobufsmt.proto and that's where that is coming from all right the name space is important not so much because it's important it's just this the naming of this i found with the protocol and the code generation and things it's a bit out of control and so you've got to name things correctly otherwise it's going to be really really confusing and i'm guessing you're going to find that out pretty quick so in this case the namespace is very intentionally grpc.services all right this will become the c class namespaces if you will and to sort of separate out your normal namespace let's say you're from your project from the code things i suggest having a different name space for these code generated classes and and types so that there's no confusion all right and don't name it like your main project like in this case grpc.movies or something that's just get too confusing okay the server calls is the movie service so that we're leaving that that way the movie response is here and the movie response is nothing but a class or a message that contains so you're not cloud it can get translated to a class but this is a proto message that contains a field called movie so the field is called movies of type movie repeated meaning it's just like an array or a collection of this movie type these the place or the position numbers here field positions are important i'll get that i'll get to that in in a bit the movie message has a title property of type string this is a protobuf type not a c-sharp type these are all product above types another property called image url which is also of type string a year property that's of type in 32 this is also proved above in 32 it's not the upper case i by the way you just notice that this is protocol of n32 and a probably called genre which is of type genre which is an enum which is defined here the [Music] field in indices or field positions there are important because in grpc when we serialize data in binary unlike in json and xml we always serialize like the name and the value like the field name and the value the feeling and the value and if you have a million records that are being sent across the wire then the same field name is being repeated a million times with maybe different value but the field name is the same right the protobufs what they've said is if you and i are talking and we both know the protocol in which we are talking so i want to send you this message let's say this movie message you already know the movie message because you've got the profile too so you know when i send you a movie that field one is title so i don't need to send you the name of the field i can just send you the value in position one i send you the value you know it's title because you and i are working off of the same contract the same idle the same interface definition language so it saves a lot of time and if you have to send 10 records back you're going to keep repeating the word title title title title 10 times and for every field right this is what json and um xml do right now bson i think binarycell.json does something similar but anyway i'm talking about just the binancialization so the position numbers are important because you can if you wanted to add more fees in the future to still remain backward compatible if you don't want the property in the future remove it but don't renumber the fields so if you remove number one field number one remove it don't rename the others what i'm saying so leave the field numbers alone or position numbers add more if you want remove old ones if you want just don't touch the things the the numbering the numbering cannot be changed to remain backward combined okay so that's the importance of that importance of that okay so here's our service we want to implement the service i'm going to go about just changing a few things here i'm going to change this file to be called movieservice.org the proto file movie service dot photo and i want to change this greeter service to movie service dot proto the cs file here okay okay now i'm just going to remove this and i don't care for this method okay so that's i don't really care for that again notice that because this is feeding off of the standard.net core asp.net framework it has access so you have access to dependency injection and all the other features configuration dependency injection everything that comes as part of the package in asp.net is available to you in jrpg services as well you're wondering why i'm talking about independence injection i just saw it there i don't necessarily use it but it's there so i'm letting you know okay so now here's the part i want to talk about the generated files so i'm going to navigate to the file folder for this project so while i'm here i can show you some of the things that changed when we was created for us when we created a service jrpc service a reference to the proto file and the fact that we want to generate the server code for this app in this project here you can have server client both and so on i'll show you that later and that's the library that's required for the grpc feature for asp.net okay okay so now i'm going to navigate to the folder of this project okay now i'm going to go into the obj folder not the bin so it's obj folder debug.net5 proto here i'm just going to delete this because it's going to get confusing now we have greet and movie service files here okay so i'm going to delete those files all right i'm going to go to the proto file here and let's say i make a change here i've got a typo and i save go back to that folder nothing here right if i remove the typo and save go back here you'll see the files just got created so anytime you modify the protofile i suggest you delete these files that are being generated so you know there's no other way to tell if you have made a mistake and typos are pretty simple to make no squiggles no code formatting nothing on the profiles yet in visual studio if you make a mistake you won't know you made a mistake so what i suggest is get to this folder this obj folder proto file folder delete these files make the changes hit save and you will see the file generate if they are generated the protofile is fine if there are any issues with the protofile syntactically or otherwise those files will not be generated so you know there's a mistake all right so that's what i did here now i'm going to open this file here the movie says grpc.cs not the other one this is called movie service and you can see it's all code generated over here notice our namespace grpc.services and then the class name movie server so that's coming from our profile isn't it that's the namespace and that's the movie service right okay but notice this class is static which means you're not going to be creating an instance of this class however this class has a nested class here this is a nested class it's not static and it's called movie server space this is our services base class so the way you're going to reference that class is of course namespace dot autoclass name dot inner class name all right let's try that so this class descends from grpc dot services dot movie service dot movie subspace that's how you get to that now i don't like such long-winded names i want to just alias this so i can use just this much okay all right so there we have it the class we want to implement on the server side has to be sent from the code gend class okay and so the implementations are up to us but there's a lot of plumbing that's done you can look at those classes yourself there's a huge amount of code that is generated by the the code generation of grpc4.net uh not these just code read but unless it's not required we need to just focus on our descending class here on the cells and implement the methods that we want so in our case if you were to just try and override some methods you'll notice that we have a method called get movies that we can override and here let me just put this into namespaces remove so the clutter okay so we have a movie called sorry a method called get movies that returns a task of movie response and if you remember movie response has a property called movies that contains the collection right the task returning just allows us to do these things asynchronously so the async modifier is not ever part of the signature of the method task is a part of significant method whether you make that implement that method asynchronously or not is up to you but you it's not considered part of the method signature in other words because we're overriding we don't know if the parent method had the async modifier on that method or not it doesn't really matter okay so our job here is to implement this so we can in one sense we can say okay we have a movie response and this is nothing but a new movie response right this movie response has a movies property here this is a collection and this is of type as you can see here of repeated field of movie the way the class is implemented this movie response you don't have the ability to replace the entire collection you can assign the collection you can add to the collection all right so just keep that in mind so here we can add right so you can say movies dot ad we just zoom out okay and here we can uh add a movie right and once we add all the bunch of movies that we want we can then finally have a return this the response right we can do that and that's that now this is a task returning method this is this is not a task so over here we'll just do a you know task start from ace from result okay and do that and that's that i'll just implement the these methods better so you get some actual movies back there you go all right so i've just added title 01 to 34 years 0 24 and so on just to keep it all unique and we have a sci-fi you have that sci-fi okay so server side is implemented what about the client for the client we'll add another project to the same solution this is just going to be a standard console application nothing fancy so you just add a new project it'll be a console application remember it'll be dotnet five once again just for the tooling purposes now when we create a client or application that is proto related to grpc related then the idea is to get that profile how do you get the profile there's many ways to get the profile in these projects it's a project reference okay you can in an organization you can have a a service that the entire enterprise uses for example let's say discovery service discovery of all services just like that hub where you go and discover all the services you have and people can search and look for services and once they find the service you can make available that profile as a url as a file what you want to choose so here we're going to just use our own project we're going to use this project's profile in this project and in the client project we're going to use the server project so i'm just going to say add service reference here we have an option for open api grpc we'll pick jrpc and the file is going to be the file from the service in the proto folder there you'll use the url if you wanted we are choosing to generate the client code using the profile the server side uses the server option you know use client and server or messages only which is just the messages but not the actual service by itself meaning the details but not the movie service but the movie message installs a bunch of packages nougat packages that are required for the client side as well as for the code gen pieces so client factory grpc client factory gft tools of course and then protobuf okay so it's done all that for us close that out so our client application is now attending to or wants to connect to the service remember that whole name spacing i talked about grpc dot services dot movie service that's going to be important here as well because the same profile right and the class we want is going to be that name in this project so i'm just going to build this project just to make sure those other files in this project's obj folder are created right they're slightly different place let's look at where they are so navigate to this projects folder and go to obj go to debug google.5 and there's no profile here a follow sorry but those files are here so they're here so if i were to delete these files okay come back to the client application do rebuild and come back here you'll find uh well i should probably do rebuild posterity build okay and this is back okay same classes just the client side of the equation right so here grpc dot services dot movie service movie service client right so you want to new up that so a couple of things here you notice that this requires a either a call invoker or a channel we want to use the channel i'm just going to move this namespaces thing here again just too long okay so we need a channel channel in your pc like a connection so first we establish a channel between you and me you and i want to talk so we establish a channel that's a big fat bite because it allows this full duplex multiplex conversation right i can send you multiple pieces of data at the same time multiple channels multiplexed used to be multiple channels and we can have this both full duplex conversation going on as if you're really smart to be able to talk on you know multiple languages at the same time right that's what grpc allows for and so this channel grpc channel your pc channel which is actually in the core jrpc net core and from here we can say from address and the from address would be the address of the server i know it to be https localhost i think port 5001 you'll have to check it doesn't work okay so this is our grpc channel and the constructor here requires the grpc channel and that's that now on the grpc channel we should have our method get movies it produces two methods we had a get movies method defined it produces the synchronous version and the asynchronous version so i'm going to use the asynchronous version here right because typically the asynchronous version also off gives you more scale not performance more scale i don't know what that is please take a look at my other video i made many many many years ago on a single radiance c sharp five dot net four five four point five so take a look at that i'm trying to explain the idea it's not performance scale scale is not the same as performance okay so this requires an argument which is of type empty so we say new empty now this empty if you remember i mentioned well she already did that they put that in this well-known types namespace i don't need all this stuff here just take them out so empty is defined in the protobox well-known types namespace and it comes back to us this is available methods we need to await it come back to us with the movie response okay so let's do that so we say movie response is equal to await and then here to modify this to be async task returning okay so now that we have a movie response the movie response has a movies property which is a collection right so we could for reach that there and now we just print out each movie so let me just write that code to print out singular movies from here and let's see all this functional okay and here print movie with this movie okay so the clan is ready the way just so you know we're going to go and another method is the streaming method so this is the simple part of it which is very much like the greeter so i said you're seeing me to write it so you understand some of the underpinnings of this thing and the next the next method we implement would be a streaming method that's going to become a little more complicated lots of gotcha this i want to you put that into this getting started piece all right now for those of you who don't know how to do this either run a client anastar from the same solution you essentially go to the solution thing here and you go to the properties of the solution that i've kind of like clicked here and already saw it but i right click here i don't use my mouse sometimes i forget to tell you let me show you okay like that okay i just hit alt enter i pick multiple startup projects i want to make the service project run first i'm going to move that up i'm going to set it to start and then the client and set it to start so both of them will start service will start first then the client and we want to make sure the services start before the client and then here so that this application stays running let's just do that okay hit f5 to run and you should see the serve startup the client stand up and we get all our movies here so we on the server side we returned five four or five movies we got the five movies here and that's being the display so this is working fine this looks like what we would expect the beauty of this is that this server could be on your machine and the client could be on my machine and i could be talking to your machine i provided ports and things of that nature it's just standard http or http 2. it's a view if i can come in or any rock if you haven't seen that video maybe you want to look at that video ngrok i'll show you how to do that how to expose an endpoint from your local machine publicly available to anyone okay now next part will be streaming if you don't know what streaming is this is about c-sharp async streaming c-sharp 8 introduced this feature i made a video on that over here please take a look at that to understand what streaming is all about in this video i'm just going to keep saying streaming with the assumption that you know what it is you may not understand what's the big deal about it if you don't know what streaming is so please watch that video first all right so introduce another method here to our profile i'm going to go into our services profile here i'm going to add one more method here so what i've done is added a rpc method called get movie stream again this takes an empty in other words it's taking nothing this returns however a stream of movie so rather than returning something that has a repeated movies collection i'm streaming the movies it's not the same thing streaming is not the same as returning a collection but a stream of movie is essentially a collection of movies that comes bit by bit not all at once but bit by bit why because you just don't have the data at this point and as soon as you get it on your end and you send it over to the client it's considered streaming this could be a whole day long process all right so streaming methods essentially are used for situations where you want to just return the data back to the client as soon as possible or is this ongoing long process that just occurs you know and as you're getting your data able to just stream that data back you know all day long 24 7 there's a connection between your clients and your server and you're just having this conversation anyway i won't explain the benefits of streaming but that's what streaming is okay so you need to now implement this method here now as soon as i hit the save on this profile you know that the file the code gen files will be recreated and now we'll have another method on it that's for the streaming feature so let's see what that looks like on the service side so on the service side i'm going to look here and if i try and attempt to override another method i'll see that i have this movie's get movie stream method now okay and this thing again it takes an empty it doesn't return anything it has a response stream and a server call context so these two are the same it's the response stream that's new so the response stream is what we will be writing to as we receive the data from wherever the implementation would be from our database as soon as we return receive some data from a database we can be returning that out to the client and the client can do what it needs to do right away stop waiting for the full set of data or we could be just receiving data from that's an iot device that sends a message every 30 seconds or 10 seconds or whatever it is and you're returning that data back to the client as soon as you get it the client is seeing the data even though you're seeing a single thing at a time it's a streaming message right so we just we write to the response stream let me show you response stream not right and the right async takes a movie as you can see here right takes a movie so i'm gonna put a movie i'm just gonna copy one of these guys here and do that okay now this is an available so i'm going to have to wait it which means also that i need to mark this method with ac modifier let it run the task all right now if i wait it here and i repeat this multiple times let's say all the zeros you're going if you remember the async streaming feature and even the year return video in in that family of videos you'll realize that each time i write this one line of code here it's going to be seen on the client right away so the client will see one movie at a time here okay i can show you that by putting a breakpoint on this methods second line okay but let's go and implement this now this method on the client side okay so on the client side we have this and what we want to be able to do now is in our movie service client we have another method oh we need to rebuild this yeah you see that here see it's not showing me the additional methods because we haven't rebuilt this project with the new profile let's do that i'm just going to hit the rebuild on the issue here come back here and there you have it good movie stream right so that code gen thing is important to understand how it works and if you make profile changes make sure you build so you can either see or use the newer feature so get movie stream what does it do it says well give me an empty and i'll give you back an async server streaming call of movie okay so let's give it the the empty and what it's going to do is return to us a async server streaming call of movie all right okay oops open close plane and once we have that we have our response stream and the response stream has methods such as move next and current this looks very much like to get enumerator it's not a numerator in that sense but it's got the same features but that's so we can use that we can do a while move next or not while not move next get current and do that so i'm going to implement that code that's the hard way of doing it i want you to understand it though because there's a feature grpc framework that you will never discover i know it's there but what if you don't know about it i want you to understand how it's going to work how would you go about doing it and then we can talk about the stuff so i'm going to write the code here i may or may not forward fast forward the the code in the editing part but bear with me so we're going to do this we're going to say while okay and then to the same response stream dot current will give us the current movie see that that's the movie this current is a movie yeah it returns a movie all right so it's also an ice async streamreader.com so this thing's an is in stream reader the response stream and keep that in mind we will be talking about that okay so current so this gives us a movie so conceptually i could just say print the movie right that's really what i want to do here and that's that what is it complaining about here why am i actually wanting to put a knot i just need to put away it because we're saying move next i'm so confused okay and i'll print this easier so while move next it runs true if it moves next come here right so if you run this client application it will of course call the first method here so we'll see like four movies up here or five movies appear first and then these movies now i could put a delay on the server side so if i went back to our implementation here i could put like a delay okay hopefully that's not confusing i still want the break point on the second stream so i want to wait after we receive the first one on the client side all right okay same thing and we'll hit f5 is going to run the client it's going on the server first then the client and you should see the output again it'll work sometimes you'll see sometimes you won't see it so it's not something you can control so there it is this time you've seen the data come here so the first movie's already arrived this method is not yet completed it's going to be returning more movies and if you were to do something like this with the thing here okay so i'm going to step here you'll see the output coming it'll always be title zero right so i go step see it came here one more movie came here did wait for a second step wait for a second step and so on so you're seeing this coming and that's really what i want to show you is this method could take its own sweet time but as soon as it does the right stream you're going to see the data here on the client okay all right so i'm just going to remove all this because there's another plan now okay so two things first let's go back to the client here uh here and i want to rewrite this in such a way that we don't end up doing this stuff like that okay so the idea is that we have in c sharp eight we have the await for each feature right and this takes an i async renewable we have an i syncing renewal but it's kind of embedded in here so i'm just going to write an extension method all right that does that implementation for you for us and then i'll show you how that looks okay all right so it's a simple extension method class the implementation is also very simple uh async stream reader right okay [Music] all right so it's an extension on the async stream reader returning an iasync numeral which then can be used in the forage and we've got the same implementation we had before except that instead of writing or printing out the current we are yield returning the current all right which means that this is an extension method so which means here in our uh here in the recommendation here we can just say all right and movie and here we do the same thing we print movie okay now the reason i'm showing you this is that one it's important to know that all this is really easy to get from a i think stream reader to an iasync renewable by having this sort of implementation right the second thing is that there is now you see it now you don't see it before but you see it now there isn't read all async method here this is has only appeared because we've included a certain namespace and so you see it the problem with the extension method is that unless you know the namespace you don't know it exists you can't discover extension methods that's the big problem i have one of big problems with extension methods is you can't discover it so what i was really trying to show you is this in the net grpc libraries we actually have a method called read all async this implementation would be something similar to this not quite sure but that basically that's what it is now i can take this out but the only reason you're seeing is because i included this namespace that was not there before so what namespace is it in well let's find out in case you want to make a note of that it's in the grpc core namespace now that jrpc core namespace was not in by default it's not used by default so you were not going to see that method unless you included the namespace so now same thing here we just got a built-in implementation this time read all async and on the service side here i want to rewrite this a little bit essentially do be something like 4 10 in this loop i'm going to do a response stream dot right async and here essentially i'm going to say new movie and there's going to be some properties here right so title and so on so forth so let me finish up this code and i'm going to loop through that n number of times and for each one i'm writing to the stream and so we'll see movies appearing one by one i'll put a delay in there i just want to use the i in the movie title so you can see that it's reading over that thing all right let's let me do that put this back here okay so just using the eye here in different places we can see it all change also by the genre i have used the mod 6 and there are six elements in the genre as you can see here on these generals and okay let's run this let's look at the outcome there's a delay between each iteration so that'll be that'll look pretty cool here first six and then one two three right and let me join now what you didn't see over here i'm just going to run this again pay and put your attention on the server side screen as well let me restart this you'll notice here when it ends the 10 iterations there you'll see it says executed right this one executed and then request finished till then it had not finished right so that's the sort of beauty of streaming now something about the real world side of things in the real world if you have the streaming endpoints then what is the client disconnects do you just keep sending stuff out and do either like what's going to happen so in the real world of course we want to do some for each kind of loop thing but you still want to know if the client disconnected and of course if you know from the grpc side we support cancellation that is as part of the gr's pc spec cancellations are supported which means the client can let the server know hey i want to i'm cancelling like you know just don't keep going so let's see how we would implement that on the server side all right so stop all this here and over here now what we want is at this point we're going to use the server context so this is the first time that we're using this concept here so the server context has a cancellation token server contacts dot [Music] better be looking cancellation token dot if cancel requests so here we can have a while loop instead of for loop okay so that'll be while not is requested then do that remove the for loop and replace with the while loop but now we don't have an i index here so let's declare and here let's increment okay same idea except this loop is now gonna be endless it's gonna just gonna keep going it'll go all day long all year long 24 7 365 until some disconnection happens all right so or the client cancels i'm going to show you that now if i didn't do this shoot i should have okay i'm just going to back up here i'm going to undo go back to the for loop all right okay we're back here i'm going to show you something i'm going to run this application from the client so let's just come into the streaming mode here i'm going to disconnect from the client watch the server side code here there you go failed right there's an exception on the server side because the client disconnected makes sense so this is what we're going to avoid so i want to now go back [Music] and implement this while loop again so while context cancellation dot is cancelled requested well not and take all of this code oops not this code this code and move it up there and we don't have a indexer increment all right time we're going to be sending this data continuously till the client disconnects essentially and there should be no errors on the server side when the client disconnects because why we are looking to see if there's a cancellation requested okay i'm going to start this again okay so this client is going to keep running you watch this over here see this executing end point and that's it like that's the last thing it's done here i want this client it's going to keep going something happens when i click on the the windows here oh there it is now okay this is moving on right it's going to 25 26 i'm going to shut down the client you keep your eye out here there it went from executing to executed this is the new line added and the request finished so it was graceful shutdown but the client told the server that it needs to cancel us ending the connection all right i hope this was not too detailed i thought it was important when i first started with grpc i got a bit stuck with the collections the streaming all this other stuff that you know the greater services was not doing so i'm hoping this has taken you a little further than that and as i said in the beginning of this video moving forward we're going to go more detail more real world more production quality code by the end of this series of videos probably one maybe two more videos in this series so i hope you enjoyed yourselves i hope you learned a few things if you have please give me a thumbs up and i'll see you next time
Info
Channel: Shiv Kumar
Views: 2,490
Rating: undefined out of 5
Keywords: gRPC, Remote Procedure Call, RPC, HTTP/2, Stubby, SPDY, WebSockets, .NET 5, .NET Core, C#, XML RPC, SOAP, DCOM, CORBA, QUIC, .NET Core 3.1, IAsyncStreamReader, IAsynEnumerable, RpcExceptions, RpcException, gRPC Streaming, Unary, gRPC Unary
Id: nd8YecX21cc
Channel Id: undefined
Length: 61min 0sec (3660 seconds)
Published: Sun Mar 14 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.