Pre-requisites
- Install Dapr CLI
- .NET 6 SDK installed.
- Docker Desktop
Create a Web API Publish Service
- Create a ASP.Net Core Web API project with .Net 6.0 framework version. Say name of the project as publisher
- Add nuget package Dapr.AspNetCore (I have used version 1.12.0)
- Add a directory say dapr-components into your project. Add file pubsub.yaml into the folder, which should look as below
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: myapp-pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
scopes:
- publisher
- subscriber
- Add below line of code into your program.cs
builder.Services.AddDaprClient();
Your program.cs should look as below
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddDaprClient();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
- Add a WebAPI controller, say it as PublishController.cs. The code looks as below
using Dapr.Client;
using Microsoft.AspNetCore.Mvc;
using System;
namespace Publisher.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class PublishController : ControllerBase
{
private readonly DaprClient daprClient;
private readonly ILogger<PublishController> logger;
public PublishController(DaprClient daprClient, ILogger<PublishController> logger)
{
this.daprClient = daprClient;
this.logger = logger;
}
[HttpPost]
public async Task Post([FromBody] string value)
{
await daprClient.PublishEventAsync("myapp-pubsub", "test-dapr-topic", $"{DateTime.Now} - {value}");
logger.LogInformation($"Published data at {DateTime.Now} - {value}");
}
}
}
Create a Web API Subscribe Service
- Create a ASP.Net Core Web API project with .Net 6.0 framework version. Say name of the project as subscriber
- Add nuget package Dapr.AspNetCore (I have used version 1.12.0)
- Add a directory say dapr-components into your project. Add file pubsub.yaml into the folder, which should look as below
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: myapp-pubsub
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
scopes:
- publisher
- subscriber
- Add below two lines of code into your program.cs
/* The call to UseCloudEvents adds CloudEvents middleware into to the
* ASP.NET Core middleware pipeline.
* This middleware will unwrap requests that use the CloudEvents structured
* format, so the receiving method can read the event payload directly. */
app.UseCloudEvents();
/* The call to MapSubscribeHandler in the endpoint routing configuration
* will add a Dapr subscribe endpoint to the application.
*
* This endpoint will respond to requests on /dapr/subscribe.
* When this endpoint is called, it will automatically find all WebAPI
* action methods decorated with the Topic attribute and instruct
* Dapr to create subscriptions for them. */
app.MapSubscribeHandler();
your program.cs should look as follows
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.UseCloudEvents();
app.MapSubscribeHandler();
app.MapControllers();
app.Run();
- Add a WebAPI controller, say it as SubscriberController.cs. The code looks as below
using Dapr;
using Microsoft.AspNetCore.Mvc;
namespace Subscriber.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class SubscriberController : ControllerBase
{
ILogger<SubscriberController> logger;
public SubscriberController(ILogger<SubscriberController> logger)
{
this.logger = logger;
}
[Topic("myapp-pubsub", "test-dapr-topic")]
[HttpPost("subscriber")]
public IActionResult Post([FromBody] string value)
{
logger.LogInformation(value);
return Ok(value);
}
}
}
Testing our web api's publish subscribe
- Open docker desktop
- Use below command to run publisher
dapr run --app-id publisher --app-port <port used in launch settings for https> --app-protocol https --dapr-http-port 3608 --resources-path dapr-components -- dotnet run
- Use below command to run subscriber
dapr run --app-id subscriber --app-port <port used in launch settings for https> --app-protocol https --dapr-http-port 3610 --resources-path dapr-components -- dotnet run
- Test using postman. Hit publisher endpoint. You can see in the logs that publisher published message and subscriber received message
Source code
Top comments (0)