CRUD using gRPC, EF Core, and ASP.NET Core (Part - 3)

In Part -1 and Part - 2 of this article you created EmployeeCRUD service definition using Protocol Buffer language and also implemented it in EmployeeCRUDService class. So, our gRPC service is now ready. In this part we will consume the service in an ASP.NET Core MVC application.

Begin by opening the GrpcService1 project you created earlier. Add a new ASP.NET Core MVC application in the same solution. Then copy the Protos folder from the GrpcService1 project into the newly created MVC application. At this stage your Solution Explorer will look like this:

Then open the MVC project's .csproj file by double clicking it in the Solution Explorer. Add the following markup in the .csproj file and also remove any other reference added to the .proto file.

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

The <Protobuf> section will tell Visual Studio that this is a .proto file and types required during the communication (Employee, Employees, Empty and so on) will be generated for you.

Next, add the following NuGet packages using the Manage NuGet Packages page.

  • Google.Protobuf
  • Grpc.Net.Client
  • Grpc.Tools

These NuGet packages are shown in the following figure after they are added to the project:

Now, open the HomeController and use GrpcService1 namespace at  the top. This is required because message types such as Empty, Employee, and Employees reside inside this namespace.

Then write the following code in the Index() action.

var channel = GrpcChannel.ForAddress
var client = new EmployeeCRUD.

The ForAddress() method of GrpcChannel class specifies the URL where the gRPC service can be found. Recollect from the previous part of this article that when you run the gRPC service project it is made available at this address.

The EmployeeCRUDClient class is automatically generated for you based on the .proto type and the markup added to the .csproj file. A channel is passed to the constructor of EmployeeCRUDClient class.

Now add the following code below the above lines:

Empty response1 = client.Insert(new Employee()
    FirstName = "Tom",
    LastName = "Jerry"

Here, you invoke the Insert() method of the EmployeeCRUD gRPC service using the client object. A new Employee to be added to the database is passed to the Insert() method.

Continue by adding the following code that modifies an existing employee.

Employee employee = client.SelectByID(
new EmployeeFilter() { EmployeeID = 1041 });

employee.FirstName = "Tom123";
employee.LastName = "Jerry123";

Empty response2 = client.Update(employee);

The above code invokes SelectByID() method of the gRPC service and retrieves an Employee whose EmployeeID is 1041 (change this EmployeeID based on your database). The FirstName and LastName properties of the Employee object are modified and Update() method is called to save the changes to the database.

You can delete an employee by adding this code:

Empty response3 = client.Delete(new EmployeeFilter()
    EmployeeID = 1062

The above code calls the Delete() method of the gRPC service by passing EmployeeFilter object. The EmployeeFilter specifies the EmployeeID to be deleted, 1062 in this case (change it as per your database values).

Now that you performed Insert(), SelectByID(), Update(), and Delete() methods let's complete the client app by calling SelectAll() method and displaying the data on the page.

So, conclude the Index() action by adding this code to the Index() action.

Employees employees = client.SelectAll
(new Empty());
return View(employees);

This code invokes the SelectAll() method and retrieves all the Employee objects. The Employees.Items property will give you access to all of them. That's what we do on the Index view.

@model GrpcService1.Employees

<table border="1" cellpadding="10">
    @foreach(var item in Model.Items)

As you can see, the code iterates through Items collection and displays EmployeeID, FirstName, and LastName of all Employee objects. The table rendered in the browser looks like this:

Before you run the application, set the GrpcService1 project to run first followed by the MVC web application. You can do that in the properties of the solution as shown below:

One the applications are started confirm the insert, update, and delete operations from the data rendered in the table.

In the next part of this series we will discuss something about deploying gRPC services and the limitations involved in the process.

That's it for now! Keep coding!!

"The Sun flowing through the right nostril and the Moon flowing through the left nostril should be seized at the time of twilights. Mind in meditation then oozes the nectar of bliss."

Bipin Joshi is an independent software consultant, trainer, author, and meditation teacher. He has been programming, meditating, and teaching for 25+ years. He conducts instructor-led online training courses in ASP.NET family of technologies for individuals and small groups. He is a published author and has authored or co-authored books for Apress and Wrox press. Having embraced the Yoga way of life he also teaches Ajapa Yoga to interested individuals. To know more about him click here.

Get connected : Facebook  Twitter  LinkedIn  YouTube

Posted On : 04 November 2019

Tags : ASP.NET ASP.NET Core SQL Server MVC C# Visual Studio