Create a gRPC Client And Server in ASP.NET Core

ASP.Net Core
ASP.Net Core

Introduction:

GRPC is an open source remote called (RPC) and the develop by the google in year 2015,it is used for run many programing languages for modern RPC environments, gRPC mostly used for c++, java,(+andoride),(ios),ruby,go,c# etc.

Wat is gRPC ?

gRPC is the type of language and framework that’s used http/2 protocol to exchange the machine language into the man language. it is highly performant by providing a compact binary format that requires low CPU usage. With gRPC, we also can call methods on a server application on a different machine as if were a local object. Nowadays, IoT applications are growing rapidly with a variety of devices/sensors and capabilities, especially small hardware specifications. gRPC becomes the first choice for communications between IoT devices.

as shown in the figure:

Another used of gRPC which is to common now a days that’s is Architecture that’s means if you choose this field so u will manage different communication between service to service ,client to service and also to the 3rd party restful service are often useful for external facing services, which are directly exposed to the consumer as restful base on the conversation text based massage which are optimized to the human , And this not good for internal communication (service to service)

Let’s see, how it works!

You can easily define the service contract by using the gRPC Interface Definition Language (IDL). In other words, as part of the service definition, you can specify the methods that can be invoked remotely and the data structure of the parameters and return types

gRPC supports four types of communications as below:

Unary RPC — the client sends a single request and gets back a single response.

Server streaming RPC — after getting request message from the client, the server sends back the stream of responses.

Client streaming RPC — opposite to “Server streaming RPC”, where clients send a sequence of messages, wait for the server to process them and receive a single response back.

Bidirectional streaming RPC — client send a stream of messages to server and server sends back a stream of messages.

Now Creating program:

We find the usual components of an ASP.NET Core project like:

The launchSettings

The appsettings.json files

The Program.cs

The Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;

namespace DemoAspNetCore3
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddGrpc();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseHsts();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapGrpcService<GreeterService>();

                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Communication with gRPC endpoints must be made through a gRPC client. To learn how to create a client, visit: https://go.microsoft.com/fwlink/?linkid=2086909");
                });
            });
        }
    }
}

The .csproj looks like:

<Project Sdk="Microsoft.NET.Sdk.Web">

  <PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
  </PropertyGroup>

  <ItemGroup>
    <Protobuf Include="Protos\greet.proto" GrpcServices="Server" />
  </ItemGroup>

  <ItemGroup>
    <PackageReference Include="Grpc.AspNetCore" Version="2.27.0" />
  </ItemGroup>

</Project>

3 Shares:
Leave a Reply

Your email address will not be published. Required fields are marked *

7 + nine =

You May Also Like