Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ private async Task OnBeforeStartAsync(BeforeStartEvent @event, CancellationToken
ModelNamedArg("--unix-domain-socket", sidecarOptions?.UnixDomainSocket),
PostOptionsArgs(Args(sidecarOptions?.Command)));

var daprCliResourceName = $"{daprSidecar.Name}-cli";
var daprCliResourceName = sidecarOptions?.SidecarName ?? $"{daprSidecar.Name}-cli";
var daprCli = new ExecutableResource(daprCliResourceName, fileName, appHostDirectory);

// Propagate WaitAnnotations from the original resource to the Dapr CLI executable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ internal sealed record DaprSidecarExportOptions
public string? RuntimePath { get; init; }
public string? SchedulerHostAddress { get; init; }
public string? UnixDomainSocket { get; init; }
public string? SidecarName { get; init; }

public DaprSidecarOptions ToDaprSidecarOptions()
{
Expand Down Expand Up @@ -93,7 +94,8 @@ public DaprSidecarOptions ToDaprSidecarOptions()
RunFile = RunFile,
RuntimePath = RuntimePath,
SchedulerHostAddress = SchedulerHostAddress,
UnixDomainSocket = UnixDomainSocket
UnixDomainSocket = UnixDomainSocket,
SidecarName = SidecarName,
};
#pragma warning restore CS0618
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,4 +241,13 @@ public string? DaprReadBufferSize
/// If specified, the Dapr sidecar will use Unix Domain Sockets for API calls.
/// </remarks>
public string? UnixDomainSocket { get; init; }


/// <summary>
/// Gets or sets the name of the Dapr sidecar.
/// </summary>
/// <remarks>
/// If specified, the Dapr sidecar name will be used.
/// </remarks>
public string? SidecarName { get; init; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> bu
[AspireExportIgnore(Reason = "Use the exported DTO-based overload instead to avoid ambiguous polyglot wrapper generation.")]
public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> builder, DaprSidecarOptions? options = null) where T : IResource
{
return builder.WithDaprSidecar(
var sidecarName = string.IsNullOrWhiteSpace(options?.SidecarName)
? $"{builder.Resource.Name}-dapr"
: options.SidecarName;

return builder.WithDaprSidecarCore(
sidecarName,
sidecarBuilder =>
{
if (options is not null)
Expand All @@ -61,11 +66,37 @@ internal static IResourceBuilder<T> WithDaprSidecarExport<T>(this IResourceBuild
/// <returns>The resource builder instance.</returns>
[AspireExport("configureDaprSidecar", MethodName = "configureDaprSidecar", Description = "Adds a Dapr sidecar to the resource and exposes it for callback configuration")]
public static IResourceBuilder<T> WithDaprSidecar<T>(this IResourceBuilder<T> builder, Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
{
return builder.WithDaprSidecarCore($"{builder.Resource.Name}-dapr", configureSidecar);
}

/// <summary>
/// Ensures that a Dapr sidecar is started for the resource with a custom sidecar name.
/// </summary>
/// <typeparam name="T">The type of the resource.</typeparam>
/// <param name="builder">The resource builder instance.</param>
/// <param name="sidecarName">The Aspire resource name for the Dapr sidecar.</param>
/// <param name="configureSidecar">A callback that can be use to configure the Dapr sidecar.</param>
/// <returns>The resource builder instance.</returns>
public static IResourceBuilder<T> WithDaprSidecar<T>(
this IResourceBuilder<T> builder,
string sidecarName,
Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
{
return builder.WithDaprSidecarCore(
string.IsNullOrWhiteSpace(sidecarName) ? $"{builder.Resource.Name}-dapr" : sidecarName,
configureSidecar);
}

private static IResourceBuilder<T> WithDaprSidecarCore<T>(
this IResourceBuilder<T> builder,
string sidecarName,
Action<IResourceBuilder<IDaprSidecarResource>> configureSidecar) where T : IResource
{
// Add Dapr is idempotent, so we can call it multiple times.
builder.ApplicationBuilder.AddDapr();

var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource($"{builder.Resource.Name}-dapr"))
var sidecarBuilder = builder.ApplicationBuilder.AddResource(new DaprSidecarResource(sidecarName))
.WithInitialState(new()
{
Properties = [],
Expand Down
21 changes: 21 additions & 0 deletions tests/CommunityToolkit.Aspire.Hosting.Dapr.Tests/DaprTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using Aspire.Hosting;
using Aspire.Hosting.Utils;
using CommunityToolkit.Aspire.Testing;
using Microsoft.AspNetCore.Http;

namespace CommunityToolkit.Aspire.Hosting.Dapr.Tests;

Expand Down Expand Up @@ -165,4 +166,24 @@ public async Task WithDaprSideCarAddsAnnotationBasedOnTheSidecarAppOptions(strin
Assert.Contains($"--app-protocol {expectedSchema}", commandline);
Assert.NotNull(container.Annotations.OfType<DaprSidecarAnnotation>());
}

[Theory]
[InlineData("MyName", "MyName")]
[InlineData(null, "name-dapr")]
public void WithDaprSidecarName(string? sidecarName, string expectedSidecarName)
{
using var builder = TestDistributedApplicationBuilder.Create();

var containerResource = builder.AddContainer("name", "image");

containerResource.WithDaprSidecar(new DaprSidecarOptions
{
SidecarName = sidecarName
});

var annotation = Assert.Single(
containerResource.Resource.Annotations.OfType<DaprSidecarAnnotation>());

Assert.Equal(expectedSidecarName, annotation.Sidecar.Name);
}
}
Loading