We all know that gRPC isn't exactly knew for dotnet, but the experience of playing around with gRPC services in the latest ASP.NET Core preview is, and it's amazing.
This morning, in less than 20 minutes, I was able to spin up a new .proto file and get to streaming data that went above and beyond the nice sample provided by Microsoft in the project template. That said, if you want to expand beyond the "Hello GreeterService" example, you'll need to know a few things@.
Creating your own .proto file(s) and exposing a service
First, assuming you created a project using the default template, check out the structure on disk. Inside of the solution folder, you'll notice a "Protos" folder. Go ahead on in and check it out!
If you crack open the "greet.proto" file in your favorite text editor (or within VS), you'll notice it's what you saw in Visual Studio in the WebApplication1.Server and WebApplication.Client projects. Go ahead and close that and so we can make our own .proto file.
Create an empty text file for now called mythings.proto (make sure explorer is set to show extensions. You want your file to be called mythings.proto, not mythings.proto.txt!)
Exposing the Services
Next, go back into Visual Studio. You should notice immediately that both the client, and the server projects show "mythings.proto", but how did this happen? To understand that, we'll need to crack open the Server and Client *.csproj files. The Server *.csproj file is shown below:
Right away, notice the first ItemGroup and what this is doing. It's scooping up any .proto files it can find and it lists them as an include. Also take care to notice how the GrpcServices attribute is set to "Server". If you open the Client *.csproj file, you'll see a similar layout, except that the GrpcServices attribute it set to "Client" instead, like in the below image:
Now that we've gone over that, go ahead and open the file from either project and you should see a blank file. Fill it in with the below (I really need to get a plugin to embed these :( )
Once this is done, make sure you rebuild your projects so that the appropriate code is generated from the gRPC tooling. Do not be alarmed that the generated code is not present within the solution explorer. A quick peak into the obj/debug folder reveals what's going on:
After that, go ahead and add a new service in your Server project called MyThingsService. Note the using at the top where we import MyThings so that we can inherit MyThingBase within our new service. If use intellisense with override, you can see that we can override the GetThingsByName method that we put into our .proto file earlier. Go ahead and override that method. Your service should look like the below image:
Now that we've got a service based on our proto file, let's change the base return statement to something more fun, while making usage of the new C# 8.0 switch pattern statement to return a variety of messages based on the name provided.
Before we can use this service, we need to register it in our Startup.cs within the server project. An example of this is already provided for the GreeterService, and we can place our service right next to it, like so:
Using the generated client to call the service
Finally, we can wireup the client and test out a few options! I've replaced the contents of the Main method in the Client projects Program.cs with our newly created client:
If you build and debug the Server project, followed by the Client project, you should get the following output:
Overall, I'm excited to see how the gRPC experience continues to evolve within ASP.NET Core 3.0. Right now, the process of getting my own services up and running felt painless. The only stumbling block that I had was that errors in .proto file generation often returned exit codes that were unclear as to the root cause of the issue. Based on my examination, there's a lot of activity happening in the gRPC dotnet Github repo from the Microsoft side of the house, and It'll be wonderful to see how gRPC grows within the dotnet community.
@ At the time of this writing, I'm using ASP.NET Core 3.0 Preview 3. As a disclaimer, EVERYTHING in this post is subject to change.