top of page

Dotnet core gRPC Service Development, Test and Deployment as Windows Service



Why should I use gRPC service; What is RPC? First of all, if you have a standard SOAP or REST service, you may question why you should switch to this technology. Of course, points such as performance and data transfer dimensions are interesting features. If you are transferring data with sim cards on thousands of devices, as in my work, the internet quota of the sim card becomes important. And of course, it is important to increase this speed on devices using 2G. Now, instead of working with TCP or HTTP api, what can I do so that I can overcome this? Remote Procedure Call (RPC) is a software communication protocol that we can use to request services without having to understand the details of the network, as it bypasses most of the protocol layers to improve performance. When a remote procedure service is called, the procedure parameters are transferred over the network to the environment where the procedure will be executed. This will increase the performance. You may have thought of it here, but what is the difference with a Local Process Call? Remote procedure calls are executed on another node of a network or computer. Local is executed by the server hosting the program.

Developed by Google, gRPC is a technology for implementing RPC APIs. One of the important aspects of gRPC is that it can provide implementation over HTTP2. HTTP/1.1 is a protocol that allows multiple HTTP requests to be sent over a single TCP. This creates a huge bottleneck for faster loading in apps. It allows using the same TCP connection for HTTP2 parallel request. It also compresses data with compressed header.

RPC allows the server to request an action in a certain format and receive the response in exactly the same format. In gRPC services, there is a file in which communication protocols are defined between the two ends. These are called proto files and they have certain standards. This proto file is created by one side and then shared with the developer at the other end, so that it can be coded in the same standard and easily.

It may be necessary to write another article for the scripting of the proto file. For now, I am sharing the “Hello World” proto presentation below. Here we have created a proto named User (UserService). We also wrote an rpc method called Search. We also made the definitions of Response and Request.

syntax = "proto3";

message SearchRequest{
    string req = 1;
}

message SearchResponse{
    int res = 1;
}

service User{
    rpc Search(SearchRequest) returns (SearchResponse){}
}

Creating a gRPC project with dotnet core. You can find how to do it very simply in the link below; https://docs.microsoft.com/en-us/dotnet/architecture/grpc-for-wcf-developers/create-project

public static IHostBuilder CreateHostBuilder(string[] args) =>           
    Host.CreateDefaultBuilder(args)               
        .UseWindowsService()               
        .ConfigureWebHostDefaults(webBuilder =>               
        {                   
            webBuilder.ConfigureKestrel(options =>                   
            {                        
                // Setup a HTTP/2 endpoint without TLS.                        
                options.ListenLocalhost(10443, listenOptions =>                       
                {                           
                    listenOptions.Protocols = HttpProtocols.Http1;                       
                });                       
                
                options.ListenLocalhost(8187, listenOptions =>                       
                {                           
                    listenOptions.Protocols = HttpProtocols.Http2;                       
                });                   
            });                   
            webBuilder.UseStartup<Startup>();               
        });

After the proto file is added to the project, you will start researching how to access this class from my project. First of all, the proto file you will write must be introduced in the project. When introducing startup.cs, it should be added to app.UseEndpoints in the Configure method for each proto as follows. If we examine the proto file we wrote above, we wrote a service called User and created a method called Search. After adding this code, when you build the project, the dotnet cs files of the proto file will be created automatically.

endpoints.MapGrpcService<UserService>();

UseWindowsService must be used to run Windows as a service. In this, it is necessary to install the Microsoft.Extensions.Hosting.WindowsServices package. For this, I share the nuget package installation command on the next line. When you call the application without TLS later, you will see that you get an error, for this you will need to define “Setup a HTTP/2 endpoint without TLS”. You will need to configure it in Program.cs as follows.

Install-Package Microsoft.Extensions.Hosting.WindowsServices

public class Program    
{        
    public static void Main(string[] args)        
    {            
        CreateHostBuilder(args).Build().Run();        
    }        
    
    public static IHostBuilder CreateHostBuilder(string[] args) =>           
        Host.CreateDefaultBuilder(args)               
            .UseWindowsService()               
            .ConfigureWebHostDefaults(webBuilder =>               
            {                   
                webBuilder.ConfigureKestrel(options =>                   
                {                        
                    // Setup a HTTP/2 endpoint without TLS.                        
                    options.ListenLocalhost(10443, listenOptions =>                       
                    {                           
                        listenOptions.Protocols = HttpProtocols.Http1;                       
                    });                       
                    
                    options.ListenLocalhost(8187, listenOptions =>                       
                    {                           
                        listenOptions.Protocols = HttpProtocols.Http2;                       
                    });                   
                });                   
                webBuilder.UseStartup<Startup>();               
            });    
    }

In order to call the service, you need to add the proto file in your project by pressing the green (+) key as shown in the screenshot below. You can call it by entering the address with the port we have given for http2 in the address field. The reason why we made this definition of Http2 is; BloomRPC needs pure HTTP/2 instead of HTTP/1.1+HTTP/2 . Therefore, we have made these definitions above.




Some possible errors; If the following error is received, the entered address is not correct.
 “error”: “14 UNAVAILABLE: failed to connect to all addresses”

 The following error occurs because the request must be made over http2. 

 “error”: "14 UNAVAILABLE: Trying to connect an http1.x server"

After the developments in the project are completed, you can install from the area where your project’s publish files are located, as follows, in order to publish it as a Windows service. Afterwards, the service can be started with the start command.

sc create TestService BinPath=C:\publish\WindowsServiceExample.exe

sc start TestService

dotnet core servisi durdurma ve silme komutları.
sc stop TestService
sc delete TestService

I hope this article will be of use to you…



Source: Medium - Tamer TÜRKSOY


The Tech Platform

bottom of page