Achraf Ben Alaya
No Result
View All Result
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • Exam Preparation
    • AZ-104
    • AZ-400
  • About
    • Resume
SUBSCRIBE
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • Exam Preparation
    • AZ-104
    • AZ-400
  • About
    • Resume
No Result
View All Result
Achraf Ben Alaya
No Result
View All Result
ADVERTISEMENT
Home Blog Cloud Azure

Dapr – Service Invocation Part 2/2

achraf by achraf
August 17, 2021
in Azure, Blog, Cloud, Dapr
3 min read
0
Dapr – Service Invocation Part 1/2
0
SHARES
376
VIEWS
Share on FacebookShare on Twitter

This part belong to a serie of articles about dapr :

  1. Introduction to dapr
  2. Service Invocation
  3. Publish-Subscribe pub/sub
  4. State management
  5. Hosting using AKS (Azure Kubernetes Services)

In the previous part, we have created our API and we have seen how to use dapr to expose it, also we have seen how to use the dapr dashboard and also to use zipkin as  tracing system .

In this part, we are going to create an MVC project as a Front-end for our solution.

For that we are going to see step by step from adding dapr nuget package to consuming the service .

Creating the MVC Project

First of all we are going to add a new MVC project to the solution as the follow image below :

next we are going to create the same model that we have in our API inside Models folder .

 

Next thing and before doing anything we need to install our DAPR NUGET PACKAGE :

Install-Package Dapr.AspNetCore -Version 1.3.0

now we need to configure ConfigureServices by adding dapr :

services.AddControllersWithViews().AddDapr();

Also inside the Configure method we need to add :

app.UseCloudEvents();

CloudEvents receive every incoming request with the content type of “application/cloudevents+json” .

Now we can go on and start creating our methods.

public interface IBroRepository 
   {
       Task<IEnumerable<Cookies>> Getcookies();

   }
public class BroRepository : IBroRepository
  {
      private readonly HttpClient _httpClient;

      public BroRepository(HttpClient httpClient)
      {
          _httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
      }

      public async Task<IEnumerable<Cookies>> Getcookies()
      {
          return await _httpClient.
                GetFromJsonAsync<IEnumerable<Cookies>>("/api/cookies");
      }
  }

As you can see inside the BroRepository we are calling the cookies API from the Getcookies() .

Now we need to register our service by adding a singleton and as you can see we are invoking an HTTP client that is calling the API using the app ID that we have created while running the service using dapr .

services.AddSingleton<IBroRepository, BroRepository>(_ => new BroRepository(DaprClient.CreateInvokeHttpClient("cookiesstoreapi")));

Now we can create our controller :

public class HomeController : Controller
  {
      private readonly ILogger<HomeController> _logger;
      private readonly IBroRepository _IBroRepository;

      public HomeController(ILogger<HomeController> logger, IBroRepository IBroRepository)
      {
          _logger = logger;
          _IBroRepository = IBroRepository;
      }

      public async Task<IActionResult> Index()
      {
          //logging info
          _logger.LogInformation("Entered Index method");
          var data = await _IBroRepository.Getcookies();
          //logging info
          _logger.LogInformation($"Returning data {JsonSerializer.Serialize(data)}");
          return View(data);
      }

 

  
  }

Next we add our view page :

@model IEnumerable<Cookies>
@{
    ViewData["Title"] = "Home Page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core and Dapr</a>.</p>
</div>

<table class="table">
    <thead>
        <tr>
            <th>GUID</th>
            <th>Name</th>
            <th>Date (C)</th>
            <th>Picture</th>
            
        </tr>
    </thead>
    <tbody>
        @foreach (var cookie in @Model)
        {
        <tr>
            <td>@cookie.ID</td>
            <td>@cookie.Name</td>
            <td>@cookie.Date</td>
            <td>
                <div style="margin: 0 auto; width: 90px">
                    <img src=@cookie.ImageUrl     alt="me" style="width: 90px" />
                </div>

            </td>

        </tr>
        }
    </tbody>
</table>

Next, we start the ASP.NET Core API , listening on HTTP port 5001, and dapr on port 50001,also we start the ASP.NET Core Client, listening on HTTP port 5002, and dapr on port 50002:

Ps: make sure Docker is up and dapr containers are running.

dapr run --app-id cookiesstoreapi --app-port 5001  --dapr-http-port 50001 dotnet run

dapr run --app-id CookiesClient --app-port 5002  --dapr-http-port 50002 dotnet run

As you can see by visiting the http://localhost:5002/ we can see our website up and running!

Dapr also supports the following features through HTTP/gRPC to implement the sidecar pattern:

  • State management.
  • Publish and subscribe.
  • Resource bindings & triggers.
  • Actor runtime.
  • Distributed tracing.
ShareTweet
Previous Post

Dapr – Service Invocation Part 1/2

Next Post

Dapr – State management (redis) Part 1/2

Related Posts

Switch to Azure Container Registry from Docker Hub
Azure

From Docker Hub, switch to Azure Container Registry & AKS

January 16, 2023
132
How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)
Azure

How to configure an ingress controller using TLS/SSL for the Azure Kubernetes Service (AKS)

November 25, 2022
229
How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)
Azure

How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)

November 23, 2022
100
How to setup nginx reverse proxy for aspnet core apps with and without  Docker compose
Azure

How to setup nginx reverse proxy for aspnet core apps with and without Docker compose

November 23, 2022
153
Win free certifications at the Microsoft Build Cloud Skills Challenge | May 2022 🎁
Blog

Win free certifications at the Microsoft Build Cloud Skills Challenge | May 2022 🎁

May 28, 2022
107
Generating report for SSL Certificates for Websites with PowerShell
Azure

Generating report for SSL Certificates for Websites with PowerShell

April 10, 2022
386
Next Post
Dapr – State management (redis)  Part 1/2

Dapr - State management (redis) Part 1/2

Leave a Reply Cancel reply

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

Terraform

Certifications

Microsoft certified trainer (MCT)

Recommended

Migrate and modernize your applications on Azure – Part –1 (Migrate Database)

Migrate and modernize your applications on Azure – Part –1 (Migrate Database)

April 3, 2021
248
How to make the most of each day

How to make the most of each day

February 2, 2021
166
Create a Linux VM with infrastructure in Azure using Terraform

Create a Linux VM with infrastructure in Azure using Terraform

August 30, 2020
1.9k
Switch to Azure Container Registry from Docker Hub

From Docker Hub, switch to Azure Container Registry & AKS

January 16, 2023
132
Xamarin.forms,Blazor and Web API

Xamarin.forms,Blazor and Web API

December 3, 2020
927
Configure postman / newman API tests in Azure DevOps

Configure postman / newman API tests in Azure DevOps

October 25, 2021
2.2k
Facebook Twitter LinkedIn Youtube
Switch to Azure Container Registry from Docker Hub

From Docker Hub, switch to Azure Container Registry & AKS

January 16, 2023
How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)

How to configure an ingress controller using TLS/SSL for the Azure Kubernetes Service (AKS)

November 25, 2022
How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)

How to setup nginx reverse proxy && load balancer for aspnet core apps with Docker and azure kubernetes service (AKS)

November 23, 2022

Categories

  • Apps (1)
  • Azure (39)
  • blazor (2)
  • Blog (62)
  • c# (7)
  • Cloud (37)
  • Dapr (4)
  • docker (3)
  • Games (1)
  • General Tips & Fix (1)
  • motivation (2)
  • Motivation (3)
  • News (9)
  • Resume (1)
  • sql (4)
  • Tricks, Tips and Fixes (3)
  • xamarin (5)
No Result
View All Result
  • News
  • Blog
    • blazor
    • c#
    • Cloud
      • Azure
    • docker
    • sql
    • xamarin
    • Dapr
    • Tricks, Tips and Fixes
  • Cloud
  • Motivation
  • General Tips & Fix
  • Exam Preparation
    • AZ-104
    • AZ-400
  • About
    • Resume