Introduction
In the realm of modern software development, building applications using microservices architecture has become increasingly popular. Microservices offer a way to break down large, monolithic applications into smaller, manageable services that can be developed, deployed, and scaled independently. And when it comes to developing microservices in the .NET ecosystem, one powerful tool that comes to mind is Dapr (Distributed Application Runtime). Dapr is an open-source, portable runtime that makes it easier for developers to build resilient, microservices-based applications. It provides a set of building blocks for building distributed systems, including service-to-service communication, state management, pub/sub messaging, and more.
Setting Up the Environment
Install Dapr CLI: Install the Dapr CLI, which allows you to interact with the Dapr runtime.
powershell -Command "iwr -useb https://raw.githubusercontent.com/dapr/cli/master/install/install.ps1 | iex"
Initialize Dapr in your local Environment: Run the init CLI command.
dapr init
Steps to Integrate Dapr into our Microservice
Step 1: Install Dapr .Net SDK
Add the Dapr .Net SDK to your project using NuGet Package Manager or .NET CLI.
dotnet add package Dapr.Client
Step 2: Register the Dapr Client in Program.cs file
builder.Service.AddControllers().AddDapr();
Step 3: Invoke Dapr in Your Controller:
using Dapr.Client;
using DaprDemo.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
namespace DaprDemo.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StateManagementController : ControllerBase
{
private readonly DaprClient daprClient;
public StateManagementController(DaprClient _daprClient)
{
daprClient = _daprClient;
}
//http://localhost:5136/api/StateManagement/SaveState
[HttpPost]
[Route("SaveState")]
public IActionResult SaveState([FromBody]StateSession stateSession)
{
daprClient.SaveStateAsync("statestore", stateSession.Key, stateSession.Value);
return Ok();
}
//http://localhost:5136/api/StateManagement/GetState/key?key=User1
[HttpGet]
[Route("GetState/key")]
public async Task<IActionResult> GetState(string key)
{
var value = await daprClient.GetStateAsync<string>("statestore", key);
return Ok(value);
}
}
}
Running the Microservice with Dapr
Start your microservice by running the following command:
dapr run --app-id DaprDemo --app-port 5136 --dapr-http-port 3500 dotnet run
Conclusion
In this blog post, we’ve covered the basics of building a .NET Core microservice with Dapr. We’ve explored setting up the environment, creating a simple microservice, integrating Dapr for state management and running the microservice with Dapr. Dapr simplifies the development of microservices by providing a set of building blocks that handle common distributed systems challenges. Whether you’re building a small-scale application or a large-scale distributed system, Dapr can help you build resilient, scalable, and maintainable microservices.