Search⌘ K
AI Features

Setting Up gRPC Client and Server Projects Locally

Explore how to set up gRPC client and server projects on your local machine using .NET. Learn to create projects via Visual Studio and CLI, configure dependencies including NuGet packages, handle Mac-specific HTTP/2 settings, and manage development TLS certificates for secure communication.

If we have all the suitable tools installed on our development machine, including the latest .NET SDK and either a code editor or IDE, we can start creating gRPC client and server applications right away. However, if we want to do it via the GUI of an IDE, rather than the command line, we might have to install specific workload components to gain access to the relevant project templates. For example, if we're using Visual Studio on Windows, we'll need to open Visual Studio Installer, click the “Modify” button next to our Visual Studio instance, find “ASP.NET and web development” under the “Workloads” tab, and install it.

Selecting the ASP.NET and web development workload
Selecting the ASP.NET and web development workload

Once the workload is enabled, we can create an application from the ASP.NET Core gRPC service template, which will be an ASP.NET Core web application with gRPC components.

Creating a gRPC service project

If we're using Visual Studio, we can create a gRPC service application by finding the “ASP.NET Core gRPC Service” template in the “Create a new project” dialog, as the following screenshot demonstrates:

ASP.NET Core gRPC Service project template
ASP.NET Core gRPC Service project template

If we're using a code editor instead of an IDE, or if we simply prefer to work with CLI, we can create a project by executing the following command (assuming that we want to call our project BasicGrpcService):

Shell
dotnet new grpc -o BasicGrpcService

Next, we'll go through setting up a gRPC client and installing all the required dependencies.

Setting up a gRPC client project

The gRPC client functionality can be added to absolutely any .NET project type, including the most basic console application. All we need is to install all of the following NuGet packages:

  • Grpc.Net.Client

  • Google.Protobuf

  • Grpc.Tools

We can install all of these packages from an IDE. If we're using a code editor instead of an IDE, we can do so via the CLI. To do so, we'll need to open any command-line terminal in the project folder and execute the following commands:

Shell
dotnet add package Grpc.Net.Client
dotnet add package Google.Protobuf
dotnet add package Grpc.Tools

If we're using an ASP.NET Core application as a gRPC client, we can register a reusable gRPC client in a dependency injection container. To do so, we'll need to install the Grpc.Net.ClientFactory NuGet package. If we do this, we don't have to install the above three packages individually because this package will already contain all of them.

Enabling gRPC development environment on Mac

If we're using Mac for our application development, we cannot use HTTPS if we want to host a gRPC application. We must use the HTTP address for gRPC communication instead. However, we have to make some additional adjustments because only the secure HTTPS protocol is mapped to an HTTP/2 port by default.

We'll need to add some code to the Program.cs file of the gRPC service application. We'll need to ensure that our Kestrel server is configured with an HTTP/2 port mapping without enforcing SSL. Below is an example of the code that can be inserted before the app variable is created. We just need to replace the first parameter in the ListenLocalhost call with the actual port value from the launchSetting.json file:

C#
builder.WebHost.ConfigureKestrel(options =>
{
options.ListenLocalhost(<the port of HTTP URL from the launchSettings.json>,
o => o.Protocols =
HttpProtocols.Http2);
});

Note that this will override the configuration of the launchSetting.json file.

Setting up the development TSL certificate

When you run an ASP.NET Core application on a development machine, the TLS encryption will be enabled by the .NET development certificate by default. If you run your application via an IDE for the first time, you will be prompted to trust this development certificate. If you're not using an IDE, you can do so by executing the following CLI command:

Shell
dotnet dev-certs https --trust

However, this command will not work on Linux. Moreover, since the process of trusting certificates on different Linux distros is different, you should refer to the manual of the Linux distro you're using (if you've chosen Linux as your development environment).