<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Prasanna</title>
    <description>The latest articles on DEV Community by Prasanna (@prasannaupatankar).</description>
    <link>https://dev.to/prasannaupatankar</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1039480%2F4eb04b5a-8bd3-458b-a498-36205ebef80c.png</url>
      <title>DEV Community: Prasanna</title>
      <link>https://dev.to/prasannaupatankar</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/prasannaupatankar"/>
    <language>en</language>
    <item>
      <title>gRPC client and .Net Core</title>
      <dc:creator>Prasanna</dc:creator>
      <pubDate>Sun, 09 Apr 2023 12:29:22 +0000</pubDate>
      <link>https://dev.to/prasannaupatankar/grpc-client-and-net-core-4gm7</link>
      <guid>https://dev.to/prasannaupatankar/grpc-client-and-net-core-4gm7</guid>
      <description>&lt;p&gt;Kindly refer to &lt;a href="https://dev.to/prasannaupatankar/grpc-and-net-core-1a8p"&gt;earlier&lt;/a&gt; post, where I have explained gRPC in detailed and have created a gRPC server application.&lt;/p&gt;

&lt;p&gt;Here we are going to create a gRPC client application. For that we will create a console application and add following packages.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;
    &amp;lt;PackageReference Include="Google.Protobuf" Version="3.22.1" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.Net.Client" Version="2.51.0" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.Net.Client.Web" Version="2.51.0" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.Tools" Version="2.52.0"&amp;gt;
      &amp;lt;PrivateAssets&amp;gt;all&amp;lt;/PrivateAssets&amp;gt;
      &amp;lt;IncludeAssets&amp;gt;runtime; build; native; contentfiles; analyzers; buildtransitive&amp;lt;/IncludeAssets&amp;gt;
    &amp;lt;/PackageReference&amp;gt;
  &amp;lt;/ItemGroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add a new &lt;code&gt;Protos&lt;/code&gt; folder and create a new empty file named company.proto.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--EOxzthgL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsipdt4mbyty0cd1xf87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--EOxzthgL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tsipdt4mbyty0cd1xf87.png" alt="proto folder" width="285" height="221"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  gRPC Service
&lt;/h3&gt;

&lt;p&gt;In the application, open the &lt;code&gt;program.cs&lt;/code&gt; file and create a method for adding the Company &amp;amp; Employee details. We will call this method from the &lt;code&gt;Main()&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;Here we will create a gRPC channel with gRPC Server's remote URL that will communicate with the gRPC server. After that we will have to create &lt;code&gt;Employee&lt;/code&gt; and &lt;code&gt;Company&lt;/code&gt; objects to pass it with the service.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var channel = GrpcChannel.ForAddress("https://localhost:5001", new GrpcChannelOptions
                {
                    HttpHandler = new GrpcWebHandler(new HttpClientHandler())
                });
                var readerClient = new Company.CompanyClient(channel);
                //Creating new employees
                var employees = new EmployeeModel[]
                {
                    new EmployeeModel
                    {
                        EmpId = 1,
                        EmpName = "Rama Bapat",
                        BirthDate = Timestamp.FromDateTime(DateTime.UtcNow.AddYears(-24).AddMonths(-4)),
                        CompanyId = 1
                    },
                    new EmployeeModel
                    {
                        EmpId = 2,
                        EmpName = "Krishna Nene",
                        BirthDate = Timestamp.FromDateTime(DateTime.UtcNow.AddYears(-22).AddMonths(-7)),
                        CompanyId = 1
                    }
                };
                //Creating new company
                var company = new CompanyModel
                {
                    CompanyId = 1,
                    ComapnyName = "Patankar Khauwale"
                };

                //Adding employees to company model
                company.Employees.Add(employees);
                //Calling the server channel
                var responseData = await readerClient.PostCompanyWithEmployeesAsync(company);

                if (responseData.Status == 1)
                { 
                    Console.WriteLine("Company &amp;amp; Employees added Successfully.");
                }
                else 
                {
                    Console.WriteLine("Company &amp;amp; Employees could not be added, please try again.");
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, just save and build the application. Following line will get added &lt;code&gt;.csproj&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;
  &amp;lt;Protobuf Include="Protos\company.proto" GrpcServices="Client" /&amp;gt;
&amp;lt;/ItemGroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once all is done, we will run both the applications and call the Server and we should get a response from the it. In our case the &lt;code&gt;Server&lt;/code&gt; will respond with an integer. We’ll see the results in console output window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--GY9PMyZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it2tes1l6ips4i5ptl7y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--GY9PMyZ_--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/it2tes1l6ips4i5ptl7y.png" alt="Colsole output" width="665" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! We just successfully built a &lt;code&gt;Client&lt;/code&gt; and &lt;code&gt;Server&lt;/code&gt; gRPC application.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Git repository for &lt;a href="https://github.com/prasannaupatankar/GRPCClient-NetCore/tree/dev"&gt;gRPC Client&lt;/a&gt; application&lt;/li&gt;
&lt;li&gt;Git repository &lt;a href="https://github.com/prasannaupatankar/GrpcServer-NetCore/tree/dev"&gt;gRPC Server&lt;/a&gt; application&lt;/li&gt;
&lt;li&gt;Official &lt;a href="https://grpc.io/"&gt;gRPC site&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Microsoft's documentation for &lt;a href="https://learn.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-3.1"&gt;gRPC on .Net Core&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@karim_manjra?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Karim MANJRA&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/tJnwbgDg84U?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>grpc</category>
      <category>netcore</category>
      <category>protobuffe</category>
      <category>grpcclient</category>
    </item>
    <item>
      <title>gRPC server and .Net Core</title>
      <dc:creator>Prasanna</dc:creator>
      <pubDate>Thu, 09 Mar 2023 07:35:09 +0000</pubDate>
      <link>https://dev.to/prasannaupatankar/grpc-and-net-core-1a8p</link>
      <guid>https://dev.to/prasannaupatankar/grpc-and-net-core-1a8p</guid>
      <description>&lt;p&gt;As developers, while building an API applications we find ourself between various choices of frameworks like &lt;em&gt;REST&lt;/em&gt;, &lt;em&gt;SOAP&lt;/em&gt;, &lt;em&gt;GraphQL&lt;/em&gt;, &lt;em&gt;HTTP API&lt;/em&gt; and more. It can be difficult to for a developer to choose the correct one that caters the needs of the application.&lt;/p&gt;

&lt;p&gt;In this post, I'd like to introduce you to gRPC, which is one of the newer approach. gRPC supports many languages like &lt;em&gt;C#/.Net&lt;/em&gt;, &lt;em&gt;C++&lt;/em&gt;, &lt;em&gt;Dart&lt;/em&gt;, &lt;em&gt;Go&lt;/em&gt;, &lt;em&gt;Java&lt;/em&gt; and other popular languages. Today we are focusing on how well gRPC integrates with ASP.Net Core.&lt;/p&gt;

&lt;p&gt;By the end of this post, you'll have a basic understanding of what gRPC is, how it works with ASP.Net Core and whether it is right for your application or not. We will implement a Client/Server application, you can find the full working code in the Git repository, linked below.&lt;/p&gt;

&lt;p&gt;Let's get started...&lt;/p&gt;

&lt;h3&gt;
  
  
  What is gRPC?
&lt;/h3&gt;

&lt;p&gt;gRPC stands for google Remote Procedure Calls. It was initially designed by Google to make distributed applications more manageable for both &lt;code&gt;Client&lt;/code&gt; and &lt;code&gt;Server&lt;/code&gt;. gRPC takes a refreshed look on the old RPC design by making it interoperable and efficient using Protocol Buffers and HTTP/2. The gRPC with Protocol Buffers is equivalent of JSON or XML with REST APIs. The HTTP/2 specification was published in 2015 and improved on the 20 year old HTTP/1.1 design.&lt;/p&gt;

&lt;h3&gt;
  
  
  What is Protocol Buffers?
&lt;/h3&gt;

&lt;p&gt;gRPC supports multiple serialization formats, but the most commonly used is Protocol Buffers. It is an open-source serialization structure specifically designed for efficiency and optimization. Data parsing with Protocol Buffers uses less CPU as the data is represented in a binary format which allows to minimize the size of encoded messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  gRPC &amp;amp; .Net Core
&lt;/h2&gt;

&lt;p&gt;From the Microsoft side the .NET team have been working for bringing gRPC to the .NET community. Earlier you had a basic library which wasn't good as other libraries nor you had any project template helping you to set up your application with gRPC. Now we have better library and better support.&lt;/p&gt;

&lt;p&gt;We’ll now create a gRPC application for both a &lt;code&gt;Server&lt;/code&gt; and in the next post I'll show you the &lt;code&gt;Client&lt;/code&gt;. While creating the gRPC application we will try to cater real life requirements as many as possible. For that we will create two independent applications one for the Client &amp;amp; one for the Server. We will send nested data from the &lt;code&gt;Client&lt;/code&gt; including integer, string and datetime and also receive the response from the &lt;code&gt;Server&lt;/code&gt; and process it.&lt;/p&gt;

&lt;h3&gt;
  
  
  gRPC Setup
&lt;/h3&gt;

&lt;p&gt;For the &lt;code&gt;Server&lt;/code&gt; we will create a new AspNetCore Web App and add following packages. You can use any other template as you wish.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;
    &amp;lt;PackageReference Include="Google.Protobuf" Version="3.22.1" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.AspNetCore" Version="2.51.0" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.AspNetCore.Web" Version="2.51.0" /&amp;gt;
    &amp;lt;PackageReference Include="Grpc.Tools" Version="2.52.0"&amp;gt;
      &amp;lt;PrivateAssets&amp;gt;all&amp;lt;/PrivateAssets&amp;gt;
      &amp;lt;IncludeAssets&amp;gt;runtime; build; native; contentfiles; analyzers; buildtransitive&amp;lt;/IncludeAssets&amp;gt;
    &amp;lt;/PackageReference&amp;gt;
  &amp;lt;/ItemGroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now add a new &lt;code&gt;Protos&lt;/code&gt; folder and create a new empty file named company.proto.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsipdt4mbyty0cd1xf87.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftsipdt4mbyty0cd1xf87.png" alt="proto folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We’ll use the Proto Buffers to map out our gRPC service, a request, and a response. We will create a Company &amp;amp; Employee nested structure and a container for the response.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;syntax = "proto3";

import "google/protobuf/timestamp.proto";

option csharp_namespace = "GrpcServer";

// The Company service definition.
service Company {
  // Sends a greeting
  rpc PostCompanyWithEmployees (CompanyModel) returns (Response);
}

// The request message containing the companies's details with employees.
message CompanyModel {
  int32 companyId = 1;
  string comapnyName = 2;
  repeated EmployeeModel employees = 3;
}

// The request message containing the employee's details.
message EmployeeModel {
  int32 empId = 1;
  string empName = 2;
  int32 companyId = 3;
  google.protobuf.Timestamp birthDate = 4;
}

// The response message containing the response status.
message Response {
  int32 status = 1;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, just save and build the application. Following line will get added &lt;code&gt;.csproj&lt;/code&gt; file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;ItemGroup&amp;gt;
  &amp;lt;Protobuf Include="Protos\company.proto" GrpcServices="Server" /&amp;gt;
&amp;lt;/ItemGroup&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should notice the GrpcServices attribute. It tells which side the application will be on, &lt;code&gt;Server&lt;/code&gt; or &lt;code&gt;Client&lt;/code&gt;. In above case, it is showing as &lt;code&gt;Server&lt;/code&gt; where as in the other application it will be as &lt;code&gt;Client&lt;/code&gt;. In case of where the &lt;code&gt;Server&lt;/code&gt; and &lt;code&gt;Client&lt;/code&gt; both are in same project it will be as &lt;code&gt;Server,Client&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now in the &lt;code&gt;Server&lt;/code&gt; application, create a method with name &lt;code&gt;PostCompanyWithEmployees&lt;/code&gt; and expecting &lt;code&gt;CompanyModel&lt;/code&gt; as input with &lt;code&gt;gRPC.Respose&lt;/code&gt; as return type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public override Task&amp;lt;Response&amp;gt; PostCompanyWithEmployees(CompanyModel request, ServerCallContext context)
        {
            try
            {
                //Add your database/API call here to save the Employee &amp;amp; Company details.
                //After successful post call return the Status as 1 else 0.
                return Task.FromResult(new Response
                {
                    Status = 1
                });
            }
            catch
            {
                return Task.FromResult(new Response
                {
                    Status = 0
                });
            }            
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have created a separate post for the Client application &lt;a href="https://dev.to/prasannaupatankar/grpc-client-and-net-core-4gm7"&gt;here&lt;/a&gt;.&lt;br&gt;
Once all is done, we will run both the applications and call the Server and we should get a response from the it. In our case the &lt;code&gt;Server&lt;/code&gt; will respond with an integer. We’ll see the results in console output window.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fit2tes1l6ips4i5ptl7y.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fit2tes1l6ips4i5ptl7y.png" alt="Colsole output"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Congratulations! We just successfully built a &lt;code&gt;Client&lt;/code&gt; and &lt;code&gt;Server&lt;/code&gt; gRPC application.&lt;/p&gt;

&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Git repository for &lt;a href="https://github.com/prasannaupatankar/GRPCClient-NetCore/tree/dev" rel="noopener noreferrer"&gt;gRPC Client&lt;/a&gt; application&lt;/li&gt;
&lt;li&gt;Git repository &lt;a href="https://github.com/prasannaupatankar/GrpcServer-NetCore/tree/dev" rel="noopener noreferrer"&gt;gRPC Server&lt;/a&gt; application&lt;/li&gt;
&lt;li&gt;Official &lt;a href="https://grpc.io/" rel="noopener noreferrer"&gt;gRPC site&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Microsoft's documentation for &lt;a href="https://learn.microsoft.com/en-us/aspnet/core/grpc/?view=aspnetcore-3.1" rel="noopener noreferrer"&gt;gRPC on .Net Core&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Cover photo by &lt;a href="https://unsplash.com/@karim_manjra?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Karim MANJRA&lt;/a&gt; on &lt;a href="https://unsplash.com/photos/tJnwbgDg84U?utm_source=unsplash&amp;amp;utm_medium=referral&amp;amp;utm_content=creditCopyText" rel="noopener noreferrer"&gt;Unsplash&lt;/a&gt;&lt;/p&gt;

</description>
      <category>grpc</category>
      <category>protobuffers</category>
      <category>netcore</category>
      <category>grpcserver</category>
    </item>
  </channel>
</rss>
