Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move resource creation in ResourceReadyEvent event #7211

Merged
merged 2 commits into from
Jan 23, 2025
Merged
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 @@ -5,7 +5,7 @@

#pragma warning disable ASPIRECOSMOS001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.
var db = builder.AddAzureCosmosDB("cosmos")
.WithDatabase("db", database => database.Containers.Add(new("entries", "/Id")))
.WithDatabase("db", database => database.Containers.Add(new("entries", "/id")))
.RunAsPreviewEmulator(e => e.WithDataExplorer());
#pragma warning restore ASPIRECOSMOS001 // Type is for evaluation purposes only and is subject to change or removal in future updates. Suppress this diagnostic to proceed.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
<ProjectReference Include="..\Aspire.Hosting.Azure\Aspire.Hosting.Azure.csproj" />
<PackageReference Include="Azure.Identity" />
<PackageReference Include="Microsoft.Azure.Cosmos" />
<PackageReference Include="AspNetCore.HealthChecks.CosmosDb" />
<PackageReference Include="Newtonsoft.Json" /> <!-- Required by Microsoft.Azure.Cosmos -->
<PackageReference Include="Azure.Provisioning" />
<PackageReference Include="Azure.Provisioning.CosmosDB" />
Expand Down

This file was deleted.

35 changes: 24 additions & 11 deletions src/Aspire.Hosting.Azure.CosmosDB/AzureCosmosDBExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
using Azure.Provisioning.KeyVault;
using Microsoft.Azure.Cosmos;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Diagnostics.HealthChecks;

namespace Aspire.Hosting;

Expand Down Expand Up @@ -97,18 +96,32 @@ private static IResourceBuilder<AzureCosmosDBResource> RunAsEmulator(this IResou
cosmosClient = CreateCosmosClient(connectionString);
});

builder.ApplicationBuilder.Eventing.Subscribe<ResourceReadyEvent>(builder.Resource, async (@event, ct) =>
{
if (cosmosClient is null)
{
throw new InvalidOperationException("CosmosClient is not initialized.");
}

await cosmosClient.ReadAccountAsync().WaitAsync(ct).ConfigureAwait(false);

foreach (var database in builder.Resource.Databases)
{
var db = (await cosmosClient.CreateDatabaseIfNotExistsAsync(database.Name, cancellationToken: ct).ConfigureAwait(false)).Database;

foreach (var container in database.Containers)
{
await db.CreateContainerIfNotExistsAsync(container.Name, container.PartitionKeyPath, cancellationToken: ct).ConfigureAwait(false);
}
}
});

// Use custom health check that also seeds the databases and containers
var healthCheckKey = $"{builder.Resource.Name}_check";
builder.ApplicationBuilder.Services.AddHealthChecks().Add(
new HealthCheckRegistration(
name: healthCheckKey,
new AzureCosmosDBEmulatorHealthCheck(
() => cosmosClient ?? throw new InvalidOperationException("CosmosClient is not initialized."),
builder.Resource.Databases.ToArray
),
failureStatus: null,
tags: null)
);
builder.ApplicationBuilder.Services.AddHealthChecks().AddAzureCosmosDB(
sp => cosmosClient ?? throw new InvalidOperationException("CosmosClient is not initialized."),
name: healthCheckKey
);

builder.WithHealthCheck(healthCheckKey);

Expand Down
Loading