Intro to gRPC in C# - How To Get Started,

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
there's a lot of buzz around G RPC right now the biggest thing I hear is that's confusing and people aren't sure when or even how to use it in this video I'm going to introduce G RPC show you how to set up and how to consume a G RPC service we will also discuss when G RPC might be the right choice and way it probably isn't the right choice the source code for today's video is available to download using the link below in the description you'll also find links there to sign up to my mailing list and for my patreon if you're a patreon member at the $10 per month level or higher as a special thank you I am giving you a G RPC cheat sheet to make your life a little easier it is a cheat sheet I've been using to help me develop my demos just make sure you get it soon the cheat sheet will only be available from now through the end of October after that it goes back in the vault now if you haven't come across me before my name is Tim quarry and it's my goal to make learning c-sharp easier one of the ways I do that is by teaching context there's a lot of tutorials out there teach you what to do I go a few steps beyond that to show you when to do it why you should or should not do it what pitfalls to avoid and what the best practices are basically I get you rave the real world if that's the type of training you're interested in subscribe to my channel and hit the little bell icon to be notified when I release new videos which is about twice a week then a four you move over to visual studio let's briefly talk through what G or PC is G RPC is a method of web communication between services think of it kind of like an API it responds to requests across the web and give the caller back the requested information the differences are in how it is set up and how it transports a data place the big differences G RPC relies on a known configuration that is shared between the client and the server think of it like a contract these contracts are called protocol buffers the other big difference between G RPC and API is that G RPC communicates using a binary stream this is much more efficient than a web server which can use JSON or XML now there are other differences but those are the private biggest - lets build a G RPC server and client and then we can discuss the other differences in the context of our application let's go to visual studio whereas start bakery a new project and the search box let's search for G RPC and we'll find G RPC service now this is a dotnet core 3.0 item so you'll have to have dotnet core 300 installed which as are the time of the recording of this video it's not quite out yet it's going up soon but I tell you watch this video it will be out so you should really get it for especially Visual Studio 2019 so G RPC service and hit next and I call this first one G RPC server that's a product name and we'll call this G RPC demo and the reason why is we're going to create a demo problem sorry a server project and a client project and while they'll be in the same solution they will not directly know about each other meaning there's going no references between the two I have been tempted to just do two separate solutions just make it very clear that these two don't talk to each other directly but I think it's been easier for us if we just do one solution and I as make sure I mentioned that a couple of times so let's create the server and says say hey you want to create G RPC service no authentication and we're not going to enable docker support so create the G RPC server and this is what it creates for us we have a proto's folder and a services folder so this is the server side of GRP see this is the equivalent of a web api where it sits and listens for calls that's the same thing this is going to do its kind sit and listen for calls let's talk through the various pieces of this first let's look at the proto file this is something new that you probably haven't seen before now GRP see it stands for remote procedure call RPC the G doesn't technically have a reason for being there except the common assumption it stands for Google because Google actually came up with this originally although it is an open standard so G RPC prototiles are the way you define the contract between the server and the client so start off with we have this syntax equals proto 3 you leave it alone what this does is it says use the the latest proto syntax or schema and so that's proto 3 this option right here just specifies the name space for this particular proto and we're using the name space of our G RPC server that's fine the package is the name of our package if you want rename it so it's greet and then we have the service and two messages let's talk through messages first so a message think of it kind of like a definition for a model in c-sharp you're just defining a file or a class that has a bunch of properties in it now it's a little bit different how you do it you say message not class and give it the message name that can be a name you want it to be then inside curly braces you specify one actually zero or more properties like so string name equals one now what this one is is it indicates the order in which this property goes so the next one will be 2 the next will be 3 after that's 4 5 6 it counts up that's all so we have a message and another message the reason they defined too here is because we're gonna take in one type and return a different one so when you call the say hello method remotely so the client says hey remote server I want to say hello they're gonna pass in a hello request which is just one item inside of that request is just name and it's of type string this say hello method when it runs will return a say hello reply message which just has one item in it which is a string and named message okay so those are our two objects one to be sent in one to be sent back up here in our service this is where we declare the greeter service this is a service that this particular proto is going to talk about it says okay RPC that indicates this is a thing you can call a remote procedure call and so the it's a method essentially and say hello you pass in the parameters which you just give it one parameter not multiple that's why we have this object always and inside we can define zero or more items and yes you can nest objects inside of this hello request so you have a complex object just like you could as a model in c-sharp so the say hello is a method here is the data is going to receive and then here is a data type it's going to return now if you were in c-sharp you might write this as public hello reply and then the name would be say hello with a Hello request when of input let's say and hatred clear braces that's the equivalent c-sharp code for what this is doing okay it's pretty close okay so now that's what this is doing now we're going to see us a couple times so you kind of get a better grip on this and I will recreate this step by step as well so if you're still going huh don't worry we'll get there in just wait explain all that first before we can then start thinking about pieces of it so this is a proto file this is our contract now we have on the server side this service so this is the greeter service and so it says greeter service which is a class it implements greed I'm sorry inherits from greeter dot greeter base okay now if you notice over here the package is called greet the service is called greeter this is actually used for something different but we pull from the service name greeter okay so we say greeter that's the service name okay so right here service name is greeter we pull from greeter dot greeter base now you may say where does that come from I don't see anywhere where it says greeter base and you were correct in fact if we hit f12 here we get this greet G RPC that's where that greet comes in so greet G RPC is in file name if you look it's stored in the obj /type /font Oh / thus is a generated file it says designer generated file or code so think of this kind of like what happens with wind forms where when we drag and drop a button out of the form behind the scenes in a hidden file it's writing c-sharp code well the same thing is true here it's just as being stored not even in our project it's being stored in the obj folder which is something gets regenerated every time you rebuilt so this file right here creates a whole bunch of code that we can then use one that is right here public abstract partial class greeter base and notice it declares a say hello method notice all the stuff that's going on here so it says public virtual global a couple of semicolons and then system dot throwing dot task taught tasks global color semicolons G RPC server dot hello reply there's a lot of stuff that's writing for us about what's happening but what this does is it creates all of the setup stuff for us behind the scenes it regenerates that automatically this is both a benefit and a curse of G RPC it's a benefit because it's just it makes it really simple we don't have to write this code which as you can see is quite ugly however if something goes wrong it can be somewhat of a pain and believe me when you first start working with G RPC something will go wrong now most likely in fact 95% of case 90% of case it's gonna be your fault that that PDF cheat sheet I talked about I'll walk you through all of different things in there but one of the things that I have a section on is here are the things that could go wrong and here's how to fix those because I ran to all of these issues as I was building my demos I get a demo working I think kula got through all my issues I tried again and things would break or not work that's because that generated file wasn't quite right and so I had to rebuild it after I made some tweaks in the system now one time just you know I did have to restart Visual Studio that solved one quirky issue I was half so there is a case for where it might be the tooling issue this is a brand new type that is just coming out in dotnet core three so there are definitely some hurdles to get over however most likely the issue is something that you forgot to do because there's a lot of little quirky areas to configure this okay so but just note that's where this comes from and you have to inherit from greeter base or the base of whatever type of service you're creating okay so now we have a constructor here which has pretty standard stuff they're bringing an ilogger notice the Pens the injection is working so it's bringing in an ilogger so you can use it and then down here this is the the fun one this is the one that is where we actually see the method in action so if you know us we have a public override say hello method that's because it's employed in greeter base already but in greeter base if you'd noticed it throws a new RPC exception if you try to actually use it it says hey I've not been implemented I'm unimplemented and that's because you need to override each method in your services you override each one with your own code so this case we're over itis say hello so that when say hello gets called this method runs now this is a very simple example where what's happening is it says is we get requests just returned tasks dot from result which means it's setting up a new task that just says hey just put the results in here is no delay or wait for it and the reason why is because it wants to allow either synchronous or asynchronous calling of the same method now the result is just a new Hello reply which that looks at a class and it and notice over here it just says a string message all lowercase over here we have message capital M and the reason why it's different is because this is more of a JavaScript type feel where we have the lower cases the camel case for things but over here in c-sharp this is where we use Pascal case which is the first letter of each word is capitalized and so it does a Pascal casing for us when it uses the c-sharp class conversion so hello reply has one property called message and we're just putting the hello and then the name that we've passed in so here is the hello request remember that has one value in it name and so again lower case we can say request dot name and that's a an object where it's got a few values in here but one of them is name and name is the the actual value we're looking for so if we pass in Timm it's very ply back hello Tim now if you were to launch this right now with all the demo code installed you're going to get this and it says hey i'm rony i'm running actually on this web address but it does not pop up a browser let me copy that and if you go there you see that communication with G RPC endpoints money made through a gr PC client that's a little easier to read so it's saying you can't just browse to the URL here that's not how G RPC works it's not like an API where you can just call it and see the the JSON values and come back and even though it's not pretty it at least gives you your values back that's not how G RPC works as the client we need to have the contract as well and create the client side of our contract so that's not going to work so we need to create a client let's right click on our solution and say add new project hi create console application and the reason why is because anything can be a client of a G RPC server doesn't have to be one particular project type console is just the easiest to do but you could have a web app that has a gr PC client or a G RPC server even but in this case just a consults we'll call those G RPC client to be clear as the way is and now we'll take out the hello world and we're going to do a console dot read line at the end just to stop it from closing now it's a few things we have to do in order to get ready for G RPC in a console application or any type of application and the first one is we need to add some nougat packages so there's three new get packages to add first you want to add Google dot protobuf protobuf stands for proto buffer or protocol buffer that's going to allow us to work with those protocol buffer files that's one the next one is G RPC net client this allows us to actually talk to G RPC servers and again I'm using pre-release stuff because of the fact that dotnet core 3 is not quite out yet and finally G RPC tools and this is going to allow us to have Visual Studio tooling that works with our settings so that's the three NuGet packages literally goes again so you can see them that's the three nougat packages you need to install okay now we've done that the next I'm going to do is right-click on my client and say add a folder I'm going to call this proto's and this is the convention for where to put your protocol buffer or protobuf files now remember I said that it's a shared contract and I literally mean shared as and I'm gonna right click on my G RPC server and say copy on the greet proto kora proto's right click and say paste now you can also point to the same directory for both of them I'm not gonna do that but you could and that would keep them fully in sync because their point of the same directory then they both have the latest changes so now I have greet dot proto copy over now here is the first place you're going to trip up you need to make sure that you set the values for this the properties for this to be a protobuf compiler public compiler pro buff is yes and the g RPC subclasses is set to client only now my tooling gives me this error property value is not valid this is one of those cutting-edge newer things that's causing a problem so I hit OK and close out not to worry because all of my settings for files are in my gr PC client dot CS proj file notice right down here item group protobuf include greet proto and the G RPC services equals server that should be client hit safe so go to the properties would just set this for us through a GUI but this is the manual way of doing it I have also seen a bug where it says client here but then it actually didn't have a closing tag here it had it like this like so and then inside here it had another tag I said well actually it's server that's wrong ok so make sure that it looks like this on your client where it says client okay and normally you should be all set that to the properties for the Greek proto but if it doesn't work that's how you fix it so that's one of those it's gonna cause you a problem issues the next thing is going to cause you an issue is when you start to let's close all these out I'll with this so we have a program that CS open and the next thing people try to do is start using it right away but you first need to do a rebuild on your client and the reason why is we that creates that file in the obj folder that has your client settings for this Greek proto so now we have that set up we should be able to start working with our gr PC server so first me I set up a channel so var channel equals gr PC channel control dot to add the using statement dot for address we have to give it a web address HTTP in my case is localhost port 5001 that's what the server operates on remember I went copied and pasted that internet browser that was the URL it may be different for you and of course you don't want to hard-code this right into your c-sharp code you're going to want to put this into either app settings that JSON or something similar so that you can change it at runtime depending on where your G RPC server is located alright so next we set up a client so a client equals new and then we have this is where we basically kind of like instantiate our our G RPC server call so new greeter control dot to add using G RPC server notice that's a using statement for this right here but it doesn't have a reference to it so it's a little bit tricky it's got a using statement for it there's no reference to it so new greeter dot greeter client I'm going to pass in across a channel so this is one of the ways that G RPC is a little bit different it tries to act like you are making a local call so this equals new greeter dot greeter client it's acting like you're instantiating this over here but you're not what you're doing is instantiate the client we're going to talk across this channel to call this on the server so again that's all set for you in the background so now you have a client you can say let's get the reply so VAR reply equals client dot and notice I have a couple of methods here say hello and say hello a sync let's say hello asynchronously because you could do it synchronously but really everything across the web is asynchronous so let's call it asynchronously now we're gonna wait this turn out wait this and I say hello async and it says okay I want you to pass in a hello request well do we have one yet no not really so I could say let's create a hello request up above so VAR inputs equals new hello requests and then we'll pass in the name equals Tim okay so create is available for us so now you can pass that in input now it's still yelling ass because it says hey wait the oh wait our operator can only be used in a sink method but we're in static void main well this hasn't been fixed in later versions of.net we can actually say static async task main control dot on the task - and the using statement and now this is an asynchronous caller which means that we can call asynchronous calls so now that we have the reply back in theory we've waited for it so now I can say console.writeline and just say reply dot message it again message is what they promise to send back in the reply now with this we can just run this and should work however we need to run both the server and the client in order for this to work so how we do that well we right-click on the solution and we say set startup projects and we'll set both of them to be start so multiple startup projects set both to be start and I'm gonna move the server up one so the server starts first we hit apply and we're gonna hit run and now I have two windows opening up there's a server there's the client notice the client Omri says hello Tim so we look at the server side we'll see execute endpoint G RPC - slash greet dot greeter slash say hello so executed that and its request finished with a 200 meaning it's sent back hello Tim that's all there is to the basics of G RPC we have a server that has a contract and that then calls has set up methods to handle servicing the contract we have a client which has calls the endpoints of the server so that's the two sides of it both have this same copy of Greek proto now what I'm going to do is I'm going to comment out all the client-side code and I am going to basically ignore the server-side code and we're going to set up a new service a new G RPC service so we can have like from the ground up what would happen so let's start by going to our let's close this out and go to our G RPC server right click on proto's and say let's add a new item or a search for proto and that gives us a protocol buffer file we're going to name this let's name this customers customers dot proto now this is pro three and the name spaceman take the proto is off there's no reason to have that I'm going to create a service service customer and notice that syntax is a little bit different we have a curly brace on the same line as customer that's more in line with JavaScript type feel and other language to do the same thing so it is different but my philosophy is to stick with what is conventional unless I have a really good reason not to that way people can easily read my code and it doesn't look jarring or different and so I'm gonna stick with the standards for how it's typically done okay so I have a service called customer I'm not gonna put anything in here yet but first want to define a few types to be able to use the first time I create is a message customer lookup I call it customer like a model and the reason I call it model at the end is because it makes a little cleaner to understand and it doesn't step on toes because I want a customer object for my customer service that's a problem so a customer model isn't a problem so customer lookup model and I'm gonna say int32 user ID equals one now right away you're saying wait wait what's in 32 that's one of the available types to me the common types are bool into 32 float double and string all of them are lowercase you may ask questions about things like date time or money both of those are a little trickier to do inside of G RPC and there that's definitely outside of scope but I can cover in this video which is already going to be quite significant but just note that there are ways of doing it but it's not nearly as simple as what we're doing here so int 32 is a number often what I will do is say int I will forget and this will not work it has to be in 32 remember that this proto file is not something that Microsoft came up with is not something that Microsoft dictates how it lays out this is something that is a standard that is used across the industry and so it is different than c-sharp okay so we have an int 32 for a customer lookup model essentially we're going to do is have a way where you pass in and say hey I want this customer IDs information so passing the customer ID of let's say 5 and you get back a full customer model so that's next message customer model now I'm going to pass in a few different value or have a few different values here just to kind of show you what it might look like so string first name equals one string last name equals two I may have string email address equals three I have a bool is alive equals four and an int 32 of age equals five notice when I say equals that's just saying that number at the end is the order in which is comes through so one two three four five those are the values this tells the protobuf system how to or actually GRP see how to encode it and decode it so that it is saying through values in the correct order order is actually very important in G RPC for our purposes we really don't care it doesn't matter what this ordering is so just order it from number one at the top all the way down to whatever the last number is don't mix and match or try and change things up keep it in the correct order okay so there's our second message now I can come up here and we can create a call so RPC get customer info we're gonna pass in customer lookup model and we're gonna return so returns plural customer model semicolon at the end this is now our complete file but not quite done yet I do want to add a second one and I'll do that a little bit but for right now I'm gonna say this is good enough well let's show you here is the fact that you kind of want to create these proto files and be done and not try and come back and modify them over and over but you can't and so I'll show you how to do that so now that I have done this I need to make sure this proto file is marked correctly you can't forget that at some point so just try and remember properties is set to none that's not right protobuf compiler now if you don't see this that means you don't have the your pc tools NuGet package installed now I said this as server only so that works I've typically not found a problem with sadness found a server only its when it's set to client only it becomes a problem now if you notice you can do a client and server it can go both ways if you wanted to but for now we're just doing a separate client from a separate server ok so now that that's a setup we can do a build on our server or rebuild and this should build out the file we need it - it's called customer so no services right-click and say add class and let's call this class the customer service and the customer service should inherit from customer daaad customer base all right I can just to follow same pattern we'll create a constructor and we'll pass in a logger so we could have that logger for use control dot out user statement here I log row type customer service call logger control dot to create initialize that and now we have a constructor do you have to do that no you don't have to but you really should be logging and so having your logger available to you is a good thing now we have this we can say public override the nice thing here is with a public override if I hit space it now shows us what truly like an override and though I want is get customer info so all that code the Hat is now done I don't have to type anything so let's remove this right here and say customer model that's the way I'm returning customer model output equals new customer model I'm gonna have an EF statement I'm actually I don't normally do this I'm like a copy and paste code from my example just to save on time and I'll talk you through it's really simple code essentially if the request dot user ID so that's what's being passed in and user ID is the value being passed in so if requests out user ID is one then populate with Jamie Smith if it's to populate with Jane Doe otherwise anything else Greg Thomas that's all it's doing so it's a really simple simulation of what a database lookup might be okay so that's where you pry to a database lookup return task dot from result output so it's doing that task top from result which allows us to return a task type and it can either be synchronous or asynchronous so again this is just dummy code but it allows us to have a lookup that populates information so that's the server side and again I am going to before I do a build I want to say one more thing it's gonna trip you up I just add customer service well if you go to startup CS come down here to our endpoints I have a map for a reader service but not for customer service that would be a problem customer service control dot like so it should be customer's service oops so let's let's rename this and do you I'll rename it everywhere yes I do so now its customers service and its customers service ok so now it looks like we've got everything lined up make sure you add this mapping for any new service that you add again that's no well as things it's gonna trip you up there's about six of them on my list of things it's gonna trip you up okay now we can do our build G RPC server make sure all builds it does now we can go to our customers dot proto right click and say copy right-click on proto's up here and say paste this is our client go to the properties for this and it says server only if I say client only I still get that same error no big deal I click on the CS proj file and just change this to client okay so now I have this setup now I can do is I can right click on my client and say rebuild rebuild and succeeded now I come over here and I can make a call to that that customers so so that's me the same so var channel equals G RPC channel for address same address and then I'm going to have a client let's let's do costs customer client equals new customer dot customer client for the channel notice how if you had both of these act at the same time you wouldn't have to recreate the channel you just have a different client per service and now I can say var apply equals a wait client dot I have my options here but get customer info async a pass in a value which I don't have yet let's create that value I could do it in line by really to see it separately so I'm going to say var inputs VAR client requested equals new and it is the I forget let's hear the name is it's going to be the customer lookup model so new customer whoops new customer lookup model there we go and the user ID equals let's start one that should be Jamie I believe and now pass in the client requested and this reply should be a actually customer so as customer we can say console.writeline and let's do our string interpolation and say customer dot first name space customer dot last name those intellisense works just fine we have some actual C sharp object let's run this notice both restarting let's run this I'll just pull over the there's a server right there behind me and there's the client and it goes ah no connection fee made because the target machine actively refused it well that's just awesome ok the first thing I do is to rebuild the solution make sure it all builds if it does let's start it again before we we panic here and found there may be something going on with this server it looks like the G RPC server is having a problem starting so let's do this let's set this as startup project just the server and let's launch it again I hit enter and now it starts I think there was something whereas locked up there's a problem when you click on the screen sometimes get pauses the server I think it brought that pause across and that was the issue so now that's starting let's go back to solution say says startup set startup projects we're gonna start multiple they're both already says start in the correct order and now we're gonna start this again I'll pull just the client over and it says Jamie Smith now if I were to stop this and I were to change this value to a two and I think as Jane Doe coming up next there we go Jane Doe so again it's it's making that call to the G RPC server and the get customer influence gain of value back so that's pretty cool and that's how we work with our a new proto file and set a new service but there's more you can do with G RPC one of the things it can do oh my gosh is entirely off wait again we're I don't want to make this a huge video with hours and hours of content I mean I'd love to but the same time you have better things to do probably so let's add one more thing to our customer service our PC and this is going to be get new customers now that's the idea here is that you are you're a client and you're saying you know I I process clients and so what I do is whenever you get a new customer comes in I'm gonna find that in the database and I'm going to say then email them a welcome coupon or something like that so the client is a thing that emails the welcome click coupon and it's it's gonna say to ask the server hey are there any new customers and I started I say yep here's the new customers and they're made 0 maybe one maybe a hundred it's gonna pass those down to the client the clients gonna do some work on them that's what gonna happen now this is gonna raise a few different issues one of them is that we don't necessarily have anything to pass in we're just asking hey are there new customers in c-sharp we'd have no parameters in GRP see it's not quite that simple now there are ways of doing it but I have found the easiest and best way of doing it is to say message and now let's just say new customer request and leave it empty okay and put that in for our value de pasiĆ³n it's an empty model and then this returns and I'm going to say it returns customer model and they say well Tim that's great but that's only one how do you return multiple that's a great question and the answer is a stream now you can also return arrays and you can do that by saying it repeats and so I could create another model in here let's just pretend this one right here and I could say I want a customer model and you can say repeats I believe it's repeats equals two like so and that would have an array of objects that's not worth doing here instead we're doing is we're saying I'm gonna return you a set of customer model I don't know how many that is and it may not be instantaneous where I'm done giving you back the list it may be that I'm going out to a database and chunking it and so I'm getting a hundred and I'm saying goes down by not done yet I get another 100 and I send those down and I'm not done yet you go back and get to the last 43 and send those down and then I'm done this is where a stream comes in this is one of the things that G RPC does well is the idea of streams you can stream in data so it's a stream here and I could stream out data and that's what I'm doing right here so I'm gonna stream out customers that's what I'm gonna do so hit save on this and I'm going to rebuild the G RPC server not the client just a server now I'm done rebuilding this I can come over to the customers service and I already have my get customer info but let's go to the end of this and then let's do a new public override and now I have get new customers now let's remove this right here and one thing we have to do is going to add a sink here oops not there here declare is specifically asynchronous I'm gonna paste in again some code just because who wants to me type this out I'm just creating a new list of customers that I'm adding three new customer objects to it Tim quarry Sue Storm and bilbo baggins with all other data now with that being done I have something to return but how I return it well this is how for each VAR us two cust in customers so how do you return this you can't say return customers that won't work because you already turn on stream which means you might not be done in fact you could do multiple for each loops and a whole bunch of other things but notice here let's unpin this let's actually make this a little bit easier to read there's a three value being passed in new customer request that's the request being passed in which is blank there's nothing in there but that's the object being passed in and then we have response stream which is interesting that's that's different all you see up here we have the request in the context that's the context down there and that's the request right there we have this new thing called response stream it's an AI server stream writer so we can say response stream dot right a sink and pass in our cust and actually should have wait that so now we are ascending one customer at a time down to the client now that we fast and it's it's not really as you may say well let's take a weight off and let's just write them all asynchronously but really again ordering is important with G RPC so allowing it to a wait for the next value is valuable but you already should have all your valuables values in c-sharp but this way you can even read from a text file where you're reading line by line and as soon as you realign you send it where the next line send it read the next line send it and so on that's all you do there is no more steps there's no return value because it's just a task not a task of a type it's essentially a void return type and so as doing is saying once this is done this clear brace is right here it gets hit that will tell G RPC we're done with this stream until then the client will continue to listen for new customers so that's how we implement a stream on the server side now we need to do is implement that same stream on the client side so let's grab our customers doc roto I'm going to control a here and copy then go our customers dot Pro here which is different control a and paste that make sure that those two files are in sync again if you pointed the same directory the same files and this would be a problem now it hasn't changed the the type so if I were a select the file and go properties it's still client only and notice down here in start-up CS I still only have these two services that's ok because I didn't add a new service I just add a new value to the existing service so now it's closed everything out because it's getting confusing otherwise let's open up program Nazi s I am going to do a rebuild on the client make sure it builds I'll leave this call here for the the first part and now I'm going to say that I want to just talk to the get new customers so I'm gonna say using let's save our call equals client which is oops customer client requested there we go client request there we go so this is our nope customer client this is where having client your name five times maybe a best idea your specific as possible you have the channel and then you have a client well using this client same client because it's still the customer object so client dot get new customers you can pass in a new request model a customer request model there we go and let's make sure I make my parens correct I don't think that's correct now so now that we have this call setup I can say while a wait call that response stream that move next so it's going to wait this stream has a control dot here I believe yep G RPC core so it's going to wait for the next item in the list until there's no more items until it says nope I'm done so now it's waiting we can it's going to grab go wait and it's going to grab the next one and if guys let's just save our cost lets us do a current customer equals call dot response stream dot current that has the current item in the response stream and now I can say let's do my my console.writeline only as current customer and let's do : space current customer email address as well just we have a little more value in there okay it's going to loop through every customer in the list and again this is a stream so it's going to continue until a stream ends once it's all done then we'll have received every customer that's a new customer so let's do this let's put a couple of console right lines let's say new customer list and then another console.writeline like so so we had a little bit of space between these two just to show there's some separation or difference there let's run this pull us over and now we have a new customer list with Tim Cory's Sue Storm and Bilbo Baggins so it has asked the G RPC server and said I'm going to stream down to you zero or more items in this case it straight down to us three items and that's all there is to streaming now it may seem like I'm just saying a list down but what if that list took a little bit of time to get so let's go over to customers service and here let's say that to get every customer we're going to await task dot delay of let's just say one second so we're going to wait for one second before we give a customer and the next one got wait a second and then give a customer just kind of show how it might actually be if we're looking something up and streaming records down okay let's try this again here we go we have Jane Doe now you have Tim Cory now Sue Storm now Billa Baggins notice that delay that came between each of them so it takes a little while and that's okay in fact if we look at the time it took it took just over three seconds three seconds which with 24 milliseconds yeah so our three seconds was a delay so as to 24 milliseconds what it actually took but that's how you would look if you were actually streaming real data like I said if you're chunking data from a database you might have some delays in there as a sense more sent more and that's okay it can wait because as well as stream does it waits until this method is complete before it closes out that stream so that's G RPC in nutshell that's how you get started in G RPC and start working with it now the questions gonna come up when would I use this and I think for me the answer is not often and it may seem weird to go through an hour's worth of explanation to say yeah you're not to use us that often but it's one of those topics that is it's got a lot of buzz around it and there's a lot of people talking about it and building demos for it but I think that the bigger question is you think through is when is this the right tool for the toolbox it's a great tool to have in the toolbox it's just a question should be when do I pull that tool out Microsoft has has come up with a couple of different use cases for this that they're kind of pushing or talking about one is when you are dealing with IOT devices the Internet of Things devices so devices that have they're really small that might be bandwidth restricted they may be memory restricted they might be hard drive space restricted those devices might be a great candidate for this this G RPC because it allows for really tight small quick communication between the client the server and back that stream might also be another really big benefit to an IOT device which might have to stream back data rather than just sending over one piece of data are multiple pieces the day and being done so that stream might be of value as well the other case this might come up is in micro services so micro services have to communicate between each other now often this is done asynchronously and in near time but not real-time using something like a message queue but sometimes you're going to have a quicker or a need for a quicker communication method so something where you get information really quickly the illustration I have here is where I have a customer ID I need more information with a customer maybe you're in the middle of a transaction the customer and you need more information that the customer has to see well that might be a time where you make AG RPC call versus creating an API for that call and it might be a little bit easier or more efficient so again efficiency is the biggest thing I think we should focus on 4G RPC because E is more efficient than a web api it can also be a lot simpler to make a small G RPC application versus a Web API there's a lot less footprint in some ways now with that being said the idea of sharing a proto file a proba file that makes things a little bit more complicated an API you say call the API there it is whereas if you have a proto file that gets out of sync that would be a problem and maintain though that synchronization is something you have to think through so at the end of the day I would say that if you have a situation where you need the speed you need the low amount of bandwidth transfer or maybe you need some that streaming capability then G RPC is probably the right choice but in general if you need a shared data across the web it's a probably a better and more full idea to use an API because there's almost every web client can consume an API almost every desktop client can consume an API almost every mobile client can consume an API that's not quite the same with G RPC now you can have a G RPC server written and let's just say we write in c-sharp you can consume that G RPC server with a client that's written in Java or go or a number of different languages because G RPC is a standard and so there is that ability to have cross application communication cross language communication in an easy manner and in a lot of ways on the client side it feels like you're just making calls to a method so there is that benefit of having the what feels like an extension of your application when in reality it's a different application and maybe even a different language okay so that's the overview and the start of G RPC give it a try try it out see what you think I love to know in the comments down below what your thoughts are where you might see yourself using this and what questions you have or thoughts you have about G RPC okay thanks for watching and as always I am Tim quarry [Music] you
Info
Channel: IAmTimCorey
Views: 128,237
Rating: undefined out of 5
Keywords: gRPC in c#, grpc c# tutorial, grpc c# example, grpc c# .net core, tim corey, iamtimcorey, intro to grpc, grpc c# .net core 3.0, .net core 3.0, .net core 3, api, grpc server, grpc client, c# gprc, protobuf, protocol buffer, c# proto, grpc.core, grpc.net.client, grpcserver
Id: QyxCX2GYHxk
Channel Id: undefined
Length: 63min 59sec (3839 seconds)
Published: Mon Sep 30 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.