I'm doing a POC on developing micro services using .NET Aspire, Dapr, Azure Service Bus, and ASP.NET Core Web API.
I have two services:
POC.AzServiceBus.SendMesssagewhich send a message to the Azure Service BusPOC.AzServiceBus.ReceiveMesssagewhich should receive a message from the Azure Service bus
I could send a message to the Azure Service and verify using Az portal.
But I'm not able to receive any message from Azure Service using the above component (POC.AzServiceBus.ReceiveMesssage).
I don't know what mistakes I'm making. Any help on this would be greatly appreciated. Thanks for your time!
I have attached a screenshot of my entire solution, code, and please let me know if you need any other details.
.NET Aspire AppHost Program.cs:
using CommunityToolkit.Aspire.Hosting.Dapr;
using System.Collections.Immutable;
var builder = DistributedApplication.CreateBuilder(args);
builder.AddProject<Projects.POC_AzServiceBus_SendMesssage>("sendmessage-api")
.WithDaprSidecar(new DaprSidecarOptions
{
AppId = "sendmessage-api-az-mq",
ResourcesPaths = ImmutableHashSet.Create("../"),
LogLevel = "warn"
});
builder.AddProject<Projects.POC_AzServiceBus_ReceiveMesssage>("receivemessage-api")
.WithDaprSidecar(new DaprSidecarOptions
{
AppId = "receivemessage-api-az-mq",
ResourcesPaths = ImmutableHashSet.Create("../"),
LogLevel = "warn"
});
[My entire solution screenshot here][2]

Publisher YAML file:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: mq-pub-sub # The name of the pubsub component used in your app code
spec:
type: pubsub.azure.servicebus.topics # Specifies pubsub component
version: v1
metadata:
- name: connectionString
value: "connectionstring HERE"
- subscriptionName: myAzServiceSubName
Subscriber YAML file:
# subscriptions-a.yaml
apiVersion: dapr.io/v2alpha1
kind: Subscription
metadata:
name: myAzServiceSubName
spec:
pubsubname: mq-pub-sub
topic: topicname
routes:
default: api/Student/consumer-a
SendMessage component in Program.cs:
var builder = WebApplication.CreateBuilder(args);
//******* Begin configuration for Dapr *******//
builder.Services.AddDaprClient(); //This requires Dapr.AspNetCore package
builder.Services.AddControllers();
//******* End configuration for Dapr *******//
var app = builder.Build();
app.UseAuthorization();
app.MapControllers();
app.Run();
SendMessage component controller:
using POC.AzServiceBus.SendMesssage.Models;
using Microsoft.AspNetCore.Mvc;
using Dapr.Client;
namespace POC.AzServiceBus.ReceiveMesssage.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class StudentController : Controller
{
private readonly DaprClient _daprClient;
public StudentController(DaprClient daprClient)
{
_daprClient = daprClient;
}
[HttpPost("StudentDetails")]
public async Task<IActionResult> OrderDetails(StudentDetails studentDetails)
{
if (studentDetails != null)
{
await _daprClient.PublishEventAsync<StudentDetails>("mq-pub-sub", "topicname here", studentDetails);
// await _serviceBus.SendMessageAsync(studentDetails);
}
return Ok(studentDetails);
}
}
}
ReceiveMessage component in Program.cs:
using POC.AzServiceBus.ReceiveMesssage.Models;
using POC.AzServiceBus.ReceiveMesssage.Services;
using Azure.Messaging.ServiceBus;
var builder = WebApplication.CreateBuilder(args);
builder.AddServiceDefaults(); //Added .NET Aspire Service default
var connectionString = builder.Configuration["ServiceBus:sb_namespace"];
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSingleton(x => new ServiceBusClient(connectionString));// <ServiceBusService>();//Service
builder.Services.AddScoped<ServiceBusService>();//Service
builder.Services.AddControllers().AddDapr(); //Code for Dapr integration
//builder.Services.AddHostedService<ServiceBusListener>(); //lister
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
app.MapDefaultEndpoints(); //Added .NET Aspire Service default
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.MapSubscribeHandler();
/*********************************
*
This will make the endpoint http://localhost:<your-app-port>/dapr/subscribe available for the consumer so it responses and
returns the available subscriptions. When the endpoint is called, it finds all the WebAPI actions decorated
with the Dapr.Topic attribute and will tell Dapr to create subscriptions for them.
*
**************************************/
app.Run();
ReceiveMessage component controller:
using POC.AzServiceBus.ReceiveMesssage.Models;
using POC.AzServiceBus.ReceiveMesssage.Services;
using Microsoft.AspNetCore.Mvc;
namespace POC.AzServiceBus.ReceiveMesssage.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class StudentController : ControllerBase
{
private readonly ServiceBusService _service;
public StudentController(ServiceBusService service)
{
_service = service;
}
[Dapr.Topic("mq-pub-sub", "topic name here")]
[HttpGet("/consumer-a")]
public async Task<IActionResult> GetMessages()
{
var messages = _service.ReceiveMessageAsync();
if (messages == null)
{
return NotFound("No Message in the queue");
}
return Ok(new { Message = messages });
}
}
}