I have a .net 8 Aspire solution, in which i configure various micro services and attach a DAPR sidecar to each of them, like so:
// Aspire configuration
var builder = DistributedApplication.CreateBuilder(args);
builder.AddDapr();
var resourcesPath = new List<string> { builder.Configuration["Dapr:ComponentsPath"]! }.ToImmutableHashSet();
var microServicesConfig = builder.Configuration.GetSection("MicroServices").Get<MicroServiceConfig[]>()!;
foreach (var config in microServicesConfig)
{
if (!config.IsEnabled) continue;
if (string.IsNullOrWhiteSpace(config.ProjectPath)) continue;
builder.AddProject(config.ContainerName, config.ProjectPath!, "http")
.WithDaprSidecar(new DaprSidecarOptions
{
AppId = config.ContainerName,
Config = configFilePath,
ResourcesPaths = resourcesPath,
MetricsPort = config.MetricsPort,
DaprHttpPort = config.DaprHttpPort,
DaprGrpcPort = config.DaprGrpcPort,
AppPort = config.AppPort,
});
}
var app = builder.Build();
await app.RunAsync();
my appsettings.json
looks like this:
{
"Dapr": {
"ComponentsPath": ".\Dapr\Components",
"ConfigPath": ".\Dapr\config.yaml"
}
"MicroServices": [
{
"ContainerName": "user-api-service",
"MetricsPort": 9092,
"DaprHttpPort": 3502,
"DaprGrpcPort": 50002,
"AppPort": 7074,
"HttpEndpoint": 5002,
"ContainerPort": 8080,
"ProjectPath": "../../user.apiService/user.apiService.csproj",
"IsEnabled": true
},
{
"ContainerName": "tenant-api-service",
"MetricsPort": 9090,
"DaprHttpPort": 3500,
"DaprGrpcPort": 50000,
"AppPort": 7238,
"HttpEndpoint": 5004,
"ContainerPort": 8080,
"ProjectPath": "../../tenant.apiService/tenant.apiService.csproj",
"IsEnabled": true
},
{
"ContainerName": "sender-api-service",
"MetricsPort": 9091,
"DaprHttpPort": 3501,
"DaprGrpcPort": 50001,
"AppPort": 7070,
"HttpEndpoint": 5001,
"ContainerPort": 8080,
"ProjectPath": "../../sender.apiService/sender.apiService.csproj",
"IsEnabled": true
}
}
This is my config.yaml
:
apiVersion: dapr.io/v1alpha1
kind: Configuration
metadata:
name: daprConfig
namespace: default
spec:
tracing:
samplingRate: "1"
zipkin:
endpointAddress: http://localhost:9411/api/v2/spans
This is the state store:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: StateStore
spec:
type: state.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
- name: actorStateStore
value: "true"
Last, these are the 2 pubsub components i want to use:
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: UserPubSub
namespace: default
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
apiVersion: dapr.io/v1alpha1
kind: Component
metadata:
name: TenantPubSub
namespace: default
spec:
type: pubsub.redis
version: v1
metadata:
- name: redisHost
value: localhost:6379
- name: redisPassword
value: ""
As you can see, i have 3 projects: user, tenant and sender.
In the Aspire configuration, I am configuring all the pubsub components on all the micro services. I tried adding only the needed ones to each microservice: user only needs UserPubSub and the StateStore, same for tenant but with TenantPubSub instead of UserPubSub, and sender needs all 3 UserPubSub, TenantPubSub and the StateStore.
The result is always the same: I send a request expecting only user to receive it, but also tenant receives it.
The flow is this:
- I call an api on sender
- sender executes some logic and then returns the result to the client
- The result from step 2 is also forwarded to user or tenant, based on which logic was executed – in this case only to user
I have an endpoint on sender to simulate the flow:
builder.MapGet
("send/{pubsub}/create/{message}", async (string pubsub, string message, [FromServices] DaprClient daprClient, CancellationToken ct) =>
{
await daprClient.PublishEventAsync(pubsub, "create", message, ct);
return Results.Ok();
});
The user micro service is subscribed to:
builder.MapPost
("user/create", [Topic("UserPubSub", "create")] async ([FromBody] string message) =>
{
if (!string.IsNullOrWhiteSpace(message)) return Results.Ok();
return Results.BadRequest();
});
While the tenant microservice is subscribed to:
builder.MapPost
("tenant/create", [Topic("TenantPubSub", "create")] async ([FromBody] string message) =>
{
if (!string.IsNullOrWhiteSpace(message)) return Results.Ok();
return Results.BadRequest();
});
I call the sender API with this request GET http://localhost:7238/send/UserPubSub/create/Hello
, so i am expecting only the user micro service to receive the message.
Somehow, also the tenant microservice is receiving it.
What am i missing?
If you need more details, I’ll be more than happy to enrich my question.