Develop gRPC client in .NET framework (gRPC Web)



Table of Contents

1. Introduction 

2. Develop gRPC client in .NET Framework

3. Using gRPC web middleware

4. Modify gRPC service to add gRPC web middleware

5. Test the client 

6. Conclusion



Introduction 

gRPC (Google Remote Procedure Call) is an open-source high-performance framework developed by Google for building distributed systems.

Develop gRPC service in .NET 7.0 -> Check out this page before continuing, which will help develop a gRPC service in .NET 7.

We will now talk about developing a gRPC client in .NET Framework which will consume the gRPC service that was created before.


Develop gRPC client in .NET Framework

Create a new project in Visual Studio by using "Console App (.NET Framework)" template.



Give a name for the service and choose a location to save the project. Choose an appropriate framework version from the dropdown list. Click on 'Create' button to create the project.




Install the following NuGet packages to the project.
1. Grpc.Net.Client
2. Google.Protobuf
3. Grpc.Tools

Install a package that will add binaries for gRPC web middleware in the project.

4. Grpc.Net.Client.Web



Create a folder in the project and name it "Protos". Now copy the "health.Protos" file to this folder which was created in the previous post for creating a gRPC service. Edit and update this file and change "csharp_namespace" to "GrpcFrameworkClient".



Set the property of protos file by editing the Project File (.csproj) and inserting an ItemGroup as below. It may not be possible to set these values in visual studio's property window for a .NET Framework project.

<ItemGroup> <Protobuf Include="Protos\health.proto" GrpcServices="Client" /> </ItemGroup>

Now build the project [Ctrl+Shift+B] which will generate C# code for the .proto file which can be used to implement the code.

Now lets update the Program.cs file as shown below to use gRPC web middleware, which will help in establishing a successful connection.

        static void Main(string[] args) { var httpHandler = new GrpcWebHandler(
                                GrpcWebMode.GrpcWebText, new HttpClientHandler()); var httpClient = new HttpClient(httpHandler); string target = $"http://localhost:4000"; var channel = GrpcChannel.ForAddress(
                                target, new GrpcChannelOptions { HttpClient = httpClient }); var client = new HealthCheck.HealthCheckClient(channel); var reply = await client.GetHealth(new HealthRequest { }); Console.WriteLine("Response received : " + reply.Message); Console.ReadKey(); }




Build the project and we are all done implementing gRPC client in .NET Framework!

Using gRPC web middleware

gRPC .NET core service template that is part of .NET SDK is configured for HTTP/2 protocol.
The client that we created in .NET Framework transmits requests in HTTP/1.1. gRPC Web which acts as a middleware works with both HTTP/1.1 and HTTP/2. To establish a successful handshake between .NET Framework client and .NET core service, gRPC web plays an important role.

Now that we have added the web package to the client project, the service has to be modified a bit as described in the next section.

Modify gRPC service to add gRPC web middleware

First step is to create a gRPC service (If not already created) - Develop gRPC service in .NET 7.0

Second step is to install gRPC Web package [Grpc.AspNetCore.Web] in the service project.



Third step is to modify Program.cs file. Update the existing code and add the changes as highlighted in the screen shot below.



Update protocol to Http1AndHttp2

Comment out the 2 lines - 

//app.MapGrpcService<HealthService>(); //app.MapGet("/", () => "Communication with gRPC endpoints ...");

Add the following lines -

app.UseRouting(); app.UseGrpcWeb(); app.UseEndpoints(endpoints => { endpoints.MapGrpcService<HealthService>().EnableGrpcWeb(); });

These changes will enable us to use gRPC Web on the service code.

Test the client

Lets test the client now by running the gRPC service first. Once the service is up and running the console shows a message similar to "Now listening on: http://0.0.0.0:4000". 4000 is the port number here which was configured in the service code.

Now RUN the gRPC .NET Framework client. Make sure to use the same port number on service and client.

We can now see that a communication is successfully established between gRPC service and .NET Framework client. The client console window now receives "Hello!!" message from service as response.



Conclusion

gRPC Web is the middleware that helps in connecting .NET Framework client with thte .NET core service. The example used here uses http end point to be exposed and not https. If the service is configured to expose http and https end points simultaneously, that will enable the service to serve multiple clients.


Related posts -
Develop gRPC service in .NET 7.0
Develop gRPC client in .NET 7.0

Comments