Skip to content

Commit 9709412

Browse files
Refactor dapr apis (#865)
* Enhance Dapr support in Aspire framework - Added project reference to `CommunityToolkit.Aspire.Hosting.Dapr.csproj` in the Azure Dapr project file. - Updated `IDistributedApplicationBuilderExtensions.cs` with documentation for Dapr-related extension methods. - Introduced `CommandLineBuilder.cs` for building Dapr CLI command line arguments. - Modified and added classes for Dapr components, improving configuration and management. - Refactored `DaprDistributedApplicationLifecycleHook` to manage Dapr sidecar lifecycles and on-demand components. - Introduced new annotations and options classes for better Dapr component configuration. - Improved organization and clarity of Dapr-related classes with additional comments and documentation. * Enhance Dapr support in Aspire framework - Added project reference to `CommunityToolkit.Aspire.Hosting.Dapr.csproj` in the Azure Dapr project file. - Updated `IDistributedApplicationBuilderExtensions.cs` with documentation for Dapr-related extension methods. - Introduced `CommandLineBuilder.cs` for building Dapr CLI command line arguments. - Modified and added classes for Dapr components, improving configuration and management. - Refactored `DaprDistributedApplicationLifecycleHook` to manage Dapr sidecar lifecycles and on-demand components. - Introduced new annotations and options classes for better Dapr component configuration. - Improved organization and clarity of Dapr-related classes with additional comments and documentation. * Integrate Azure Dapr support into CommunityToolkit This commit introduces Azure Dapr support within the CommunityToolkit.Aspire.Hosting framework. Key changes include: - Added setup for Azure Container Apps and Redis state management in Program.cs, enhancing Dapr sidecar configuration. - Modified AzureDaprPublishingHelper to implement specific Dapr publishing requirements. - Updated IDistributedApplicationBuilderExtensions with lifecycle hooks and improved configuration options. - Introduced azure.yaml for defining the Azure Container App environment and project structure. - Added documentation in next-steps.md to guide users on post-initialization steps for Azure deployment. - Created AzureContainerAppEnvironmentResourceBuilderExtensions for configuring Azure Container App resources for Dapr. - Updated .gitignore to exclude Azure-related files for a cleaner repository. * Enhance Azure Container App Dapr integration Updated `Program.cs` to add Dapr components configuration through the new `WithDaprComponents()` method. Renamed `WithDapr()` to `WithDaprComponents()` in `AzureContainerAppEnvironmentResourceBuilderExtensions.cs`, and restructured the method to prepare for Dapr component handling, with the implementation currently commented out for future development. * Enhance Dapr support in Aspire framework - Added project reference to `CommunityToolkit.Aspire.Hosting.Dapr.csproj` in the Azure Dapr project file. - Updated `IDistributedApplicationBuilderExtensions.cs` with documentation for Dapr-related extension methods. - Introduced `CommandLineBuilder.cs` for building Dapr CLI command line arguments. - Modified and added classes for Dapr components, improving configuration and management. - Refactored `DaprDistributedApplicationLifecycleHook` to manage Dapr sidecar lifecycles and on-demand components. - Introduced new annotations and options classes for better Dapr component configuration. - Improved organization and clarity of Dapr-related classes with additional comments and documentation. * Enhance Dapr support in Aspire framework - Added project reference to `CommunityToolkit.Aspire.Hosting.Dapr.csproj` in the Azure Dapr project file. - Updated `IDistributedApplicationBuilderExtensions.cs` with documentation for Dapr-related extension methods. - Introduced `CommandLineBuilder.cs` for building Dapr CLI command line arguments. - Modified and added classes for Dapr components, improving configuration and management. - Refactored `DaprDistributedApplicationLifecycleHook` to manage Dapr sidecar lifecycles and on-demand components. - Introduced new annotations and options classes for better Dapr component configuration. - Improved organization and clarity of Dapr-related classes with additional comments and documentation. * Integrate Azure Dapr support into CommunityToolkit This commit introduces Azure Dapr support within the CommunityToolkit.Aspire.Hosting framework. Key changes include: - Added setup for Azure Container Apps and Redis state management in Program.cs, enhancing Dapr sidecar configuration. - Modified AzureDaprPublishingHelper to implement specific Dapr publishing requirements. - Updated IDistributedApplicationBuilderExtensions with lifecycle hooks and improved configuration options. - Introduced azure.yaml for defining the Azure Container App environment and project structure. - Added documentation in next-steps.md to guide users on post-initialization steps for Azure deployment. - Created AzureContainerAppEnvironmentResourceBuilderExtensions for configuring Azure Container App resources for Dapr. - Updated .gitignore to exclude Azure-related files for a cleaner repository. * Enhance Azure Container App Dapr integration Updated `Program.cs` to add Dapr components configuration through the new `WithDaprComponents()` method. Renamed `WithDapr()` to `WithDaprComponents()` in `AzureContainerAppEnvironmentResourceBuilderExtensions.cs`, and restructured the method to prepare for Dapr component handling, with the implementation currently commented out for future development. * Refactor Dapr integration for Azure Redis Cache Enhance Dapr component integration by improving method chaining in `Program.cs`, adding metadata handling for dynamic configuration, and introducing lifecycle management methods. Update unit tests for new functionalities, remove redundant code, and improve test structure. Support managed identity and access key authentication for Redis, and enhance the `WithMetadata` method to accept various metadata types. * Delete examples/dapr/CommunityToolkit.Aspire.Hosting.Azure.Dapr.AppHost/.gitignore * Delete examples/dapr/CommunityToolkit.Aspire.Hosting.Azure.Dapr.AppHost/azure.yaml * Delete examples/dapr/CommunityToolkit.Aspire.Hosting.Azure.Dapr.AppHost/next-steps.md * Add tests for Dapr component references and sidecar functionality * remove sln file * Enhance Dapr sidecar configuration with default AppId and options handling * Refactor Dapr sidecar configuration to simplify AppId handling and remove redundant checks
1 parent 11f941e commit 9709412

File tree

48 files changed

+1011
-908
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1011
-908
lines changed
Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,33 @@
11
var builder = DistributedApplication.CreateBuilder(args);
22

3+
builder.AddAzureContainerAppEnvironment("cae").WithDaprComponents();
34

4-
var redis = builder.AddAzureRedis("redisState").WithAccessKeyAuthentication().RunAsContainer();
5+
var redis = builder.AddAzureRedis("redisState")
6+
.RunAsContainer();
57

6-
// local development still uses dapr redis state container
8+
// State store using Redis
79
var stateStore = builder.AddDaprStateStore("statestore")
8-
.WithReference(redis);
10+
.WithReference(redis)
11+
.WithMetadata("actorStateStore", "true");
912

13+
// PubSub also using Redis - for Azure deployment this will use the same Redis instance
1014
var pubSub = builder.AddDaprPubSub("pubsub")
11-
.WithMetadata("redisHost", "localhost:6379")
15+
.WithReference(redis)
1216
.WaitFor(redis);
1317

1418

1519
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceA>("servicea")
16-
.WithReference(stateStore)
17-
.WithReference(pubSub)
18-
.WithDaprSidecar()
20+
.PublishAsAzureContainerApp((i,c)=> { })
21+
.WithDaprSidecar(sidecar => sidecar.WithReference(stateStore).WithReference(pubSub))
1922
.WaitFor(redis);
2023

2124
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceB>("serviceb")
22-
.WithReference(pubSub)
23-
.WithDaprSidecar()
25+
.WithDaprSidecar(sidecar => sidecar.WithReference(pubSub))
2426
.WaitFor(redis);
2527

2628
// console app with no appPort (sender only)
2729
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceC>("servicec")
28-
.WithReference(stateStore)
29-
.WithDaprSidecar()
30+
.WithDaprSidecar(sidecar => sidecar.WithReference(stateStore))
3031
.WaitFor(redis);
3132

3233
builder.Build().Run();

examples/dapr/CommunityToolkit.Aspire.Hosting.Dapr.AppHost/Program.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@
2020
.WaitFor(redis);
2121

2222
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceA>("servicea")
23-
.WithReference(stateStore)
24-
.WithReference(pubSub)
25-
.WithDaprSidecar()
26-
.WaitFor(pubSub);
23+
.WithDaprSidecar(sidecar =>
24+
{
25+
sidecar.WithReference(stateStore).WithReference(pubSub);
26+
}).WaitFor(redis);
2727

2828
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceB>("serviceb")
29-
.WithReference(pubSub)
30-
.WithDaprSidecar()
31-
.WaitFor(pubSub);
29+
.WithDaprSidecar(sidecar => sidecar.WithReference(pubSub))
30+
.WaitFor(redis);
3231

3332
// console app with no appPort (sender only)
3433
builder.AddProject<Projects.CommunityToolkit_Aspire_Hosting_Dapr_ServiceC>("servicec")
35-
.WithReference(stateStore)
36-
.WithDaprSidecar()
37-
.WaitFor(stateStore);
34+
.WithDaprSidecar(sidecar => sidecar.WithReference(stateStore))
35+
.WaitFor(redis);
3836

3937
builder.Build().Run();

src/CommunityToolkit.Aspire.Hosting.Azure.Dapr.Redis/AzureRedisCacheDaprHostingExtensions.cs

Lines changed: 171 additions & 100 deletions
Large diffs are not rendered by default.
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Aspire.Hosting.ApplicationModel;
2+
using Aspire.Hosting.Azure;
3+
using Aspire.Hosting.Azure.AppContainers;
4+
using Azure.Provisioning.AppContainers;
5+
using CommunityToolkit.Aspire.Hosting.Azure.Dapr;
6+
using CommunityToolkit.Aspire.Hosting.Dapr;
7+
8+
namespace Aspire.Hosting;
9+
10+
/// <summary>
11+
/// Provides extension methods for configuring Azure Container App Environment resources.
12+
/// </summary>
13+
public static class AzureContainerAppEnvironmentResourceBuilderExtensions
14+
{
15+
/// <summary>
16+
/// Configures the Azure Container App Environment resource to use Dapr.
17+
/// </summary>
18+
/// <param name="builder"></param>
19+
/// <returns></returns>
20+
public static IResourceBuilder<AzureContainerAppEnvironmentResource> WithDaprComponents(
21+
this IResourceBuilder<AzureContainerAppEnvironmentResource> builder)
22+
{
23+
builder.ApplicationBuilder.AddDapr(c =>
24+
{
25+
c.PublishingConfigurationAction = (IResource resource, DaprSidecarOptions? daprSidecarOptions) =>
26+
{
27+
var configureAction = (AzureResourceInfrastructure infrastructure, ContainerApp containerApp) =>
28+
{
29+
containerApp.Configuration.Dapr = new ContainerAppDaprConfiguration
30+
{
31+
AppId = daprSidecarOptions?.AppId ?? resource.Name,
32+
AppPort = daprSidecarOptions?.AppPort ?? 8080,
33+
IsApiLoggingEnabled = daprSidecarOptions?.EnableApiLogging ?? false,
34+
LogLevel = daprSidecarOptions?.LogLevel?.ToLower() switch
35+
{
36+
"debug" => ContainerAppDaprLogLevel.Debug,
37+
"warn" => ContainerAppDaprLogLevel.Warn,
38+
"error" => ContainerAppDaprLogLevel.Error,
39+
_ => ContainerAppDaprLogLevel.Info
40+
},
41+
AppProtocol = daprSidecarOptions?.AppProtocol?.ToLower() switch
42+
{
43+
"grpc" => ContainerAppProtocol.Grpc,
44+
_ => ContainerAppProtocol.Http,
45+
},
46+
IsEnabled = true
47+
};
48+
};
49+
50+
resource.Annotations.Add(new AzureContainerAppCustomizationAnnotation(configureAction));
51+
};
52+
});
53+
54+
return builder.ConfigureInfrastructure(infrastructure =>
55+
{
56+
var daprComponentResources = builder.ApplicationBuilder.Resources.OfType<IDaprComponentResource>();
57+
58+
foreach (var daprComponentResource in daprComponentResources)
59+
{
60+
daprComponentResource.TryGetLastAnnotation<AzureDaprComponentPublishingAnnotation>(out var publishingAnnotation);
61+
62+
publishingAnnotation?.PublishingAction(infrastructure);
63+
}
64+
});
65+
}
66+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using Aspire.Hosting.ApplicationModel;
2+
using Aspire.Hosting.Azure;
3+
4+
namespace CommunityToolkit.Aspire.Hosting.Azure.Dapr;
5+
/// <summary>
6+
/// Represents an annotation that defines a publishing action for Azure Dapr components.
7+
/// </summary>
8+
/// <remarks>This annotation is used to specify a custom action that is executed during the publishing process of
9+
/// Azure Dapr components. The action is applied to the provided <see cref="AzureResourceInfrastructure"/> instance,
10+
/// allowing customization of the resource infrastructure.</remarks>
11+
/// <param name="PublishingAction">The action to be executed on the <see cref="AzureResourceInfrastructure"/> during the publishing process. This
12+
/// action allows for customization of the infrastructure configuration.</param>
13+
public record AzureDaprComponentPublishingAnnotation(Action<AzureResourceInfrastructure> PublishingAction) : IResourceAnnotation;

src/CommunityToolkit.Aspire.Hosting.Azure.Dapr/AzureDaprHostingExtensions.cs

Lines changed: 2 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using Azure.Provisioning.AppContainers;
44
using Azure.Provisioning.Expressions;
55
using Azure.Provisioning;
6-
using Azure.Provisioning.KeyVault;
76
using CommunityToolkit.Aspire.Hosting.Dapr;
87

98
namespace Aspire.Hosting;
@@ -14,30 +13,6 @@ namespace Aspire.Hosting;
1413
public static class AzureDaprHostingExtensions
1514
{
1615

17-
18-
/// <summary>
19-
/// Adds an Azure Dapr resource to the resource builder.
20-
/// </summary>
21-
/// <param name="builder">The resource builder.</param>
22-
/// <param name="name">The name of the Dapr resource.</param>
23-
/// <param name="configureInfrastructure">The action to configure the Azure resource infrastructure.</param>
24-
/// <returns>The updated resource builder.</returns>
25-
public static IResourceBuilder<AzureDaprComponentResource> AddAzureDaprResource(
26-
this IResourceBuilder<AzureDaprComponentResource> builder,
27-
[ResourceName] string name,
28-
Action<AzureResourceInfrastructure> configureInfrastructure)
29-
{
30-
ArgumentNullException.ThrowIfNull(builder, nameof(builder));
31-
ArgumentException.ThrowIfNullOrEmpty(name, nameof(name));
32-
ArgumentNullException.ThrowIfNull(configureInfrastructure, nameof(configureInfrastructure));
33-
34-
var azureDaprComponentResource = new AzureDaprComponentResource(name, configureInfrastructure);
35-
36-
return builder.ApplicationBuilder
37-
.AddResource(azureDaprComponentResource)
38-
.WithManifestPublishingCallback(azureDaprComponentResource.WriteToManifest);
39-
}
40-
4116
/// <summary>
4217
/// Adds an Azure Dapr resource to the resource builder.
4318
/// </summary>
@@ -63,49 +38,6 @@ public static IResourceBuilder<AzureDaprComponentResource> AddAzureDaprResource(
6338
.WithManifestPublishingCallback(azureDaprComponentResource.WriteToManifest);
6439
}
6540

66-
/// <summary>
67-
/// Configures the infrastructure for a Dapr component in a container app managed environment.
68-
/// </summary>
69-
/// <param name="builder"></param>
70-
/// <param name="daprComponent">The Dapr component to configure.</param>
71-
/// <param name="parameters">The parameters to provide to the component</param>
72-
/// <param name="requireScopes">Whether scopes need to be assigned to the component</param>
73-
/// <returns>An action to configure the Azure resource infrastructure.</returns>
74-
public static Action<AzureResourceInfrastructure> GetInfrastructureConfigurationAction(
75-
this IResourceBuilder<IDaprComponentResource> builder,
76-
ContainerAppManagedEnvironmentDaprComponent daprComponent,
77-
IEnumerable<ProvisioningParameter>? parameters = null, bool? requireScopes = false) =>
78-
(AzureResourceInfrastructure infrastructure) =>
79-
{
80-
ArgumentNullException.ThrowIfNull(daprComponent, nameof(daprComponent));
81-
ArgumentNullException.ThrowIfNull(infrastructure, nameof(infrastructure));
82-
83-
ProvisioningVariable resourceToken = new("resourceToken", typeof(string))
84-
{
85-
Value = BicepFunction.GetUniqueString(BicepFunction.GetResourceGroup().Id)
86-
};
87-
88-
infrastructure.Add(resourceToken);
89-
90-
var containerAppEnvironment = ContainerAppManagedEnvironment.FromExisting("containerAppEnvironment");
91-
containerAppEnvironment.Name = BicepFunction.Interpolate($"cae-{resourceToken}");
92-
93-
infrastructure.Add(containerAppEnvironment);
94-
daprComponent.Parent = containerAppEnvironment;
95-
96-
97-
if (requireScopes == true)
98-
{
99-
builder.AddScopes(daprComponent);
100-
}
101-
infrastructure.Add(daprComponent);
102-
103-
foreach (var parameter in parameters ?? [])
104-
{
105-
infrastructure.Add(parameter);
106-
}
107-
};
108-
10941
/// <summary>
11042
/// Adds scopes to the specified Dapr component in a container app managed environment.
11143
/// </summary>
@@ -114,11 +46,11 @@ public static Action<AzureResourceInfrastructure> GetInfrastructureConfiguration
11446
public static void AddScopes(this IResourceBuilder<IDaprComponentResource> builder, ContainerAppManagedEnvironmentDaprComponent daprComponent)
11547
{
11648
daprComponent.Scopes = [];
49+
11750
foreach (var resource in builder.ApplicationBuilder.Resources)
11851
{
119-
12052
if (!resource.TryGetLastAnnotation<DaprSidecarAnnotation>(out var daprAnnotation) ||
121-
!resource.TryGetAnnotationsOfType<DaprComponentReferenceAnnotation>(out var daprComponentReferenceAnnotations))
53+
!daprAnnotation.Sidecar.TryGetAnnotationsOfType<DaprComponentReferenceAnnotation>(out var daprComponentReferenceAnnotations))
12254
{
12355
continue;
12456
}
@@ -135,13 +67,10 @@ public static void AddScopes(this IResourceBuilder<IDaprComponentResource> build
13567
var appId = sidecarOptions?.AppId ?? resource.Name;
13668
daprComponent.Scopes.Add(appId);
13769
}
138-
13970
}
14071
}
14172
}
14273

143-
144-
14574
/// <summary>
14675
/// Creates a new Dapr component for a container app managed environment.
14776
/// </summary>

src/CommunityToolkit.Aspire.Hosting.Azure.Dapr/AzureDaprPublishingHelper.cs

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)