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

[New article]: Document child resource support in hosting and client generations #2907

Closed
captainsafia opened this issue Mar 31, 2025 · 2 comments · Fixed by #2870
Closed
Assignees
Labels
area-docs doc-idea Indicates issues that are suggestions for new topics [org][type][category] 📌 seQUESTered Identifies that an issue has been imported into Quest.
Milestone

Comments

@captainsafia
Copy link
Member

captainsafia commented Mar 31, 2025

Proposed topic or title

Child Resource Integration in .NET Aspire 9.2

Location in table of contents.

/ Integrations / Azure / Azure Cosmos DB
/ Integrations / Azure / Azure Service Bus
/ Integrations / Azure / Azure WebPub Sub

Reason for the article

In 9.1, we adeded support for modeling child resource dependencies on the host. In 9.2, we added support for modeling child resources on the client integrations. This issue outlines documenting the client integration portion.

Article abstract

Child resource support on the client integration side is a net new feature for:

  • Azure Cosmos DB integration
  • Azure WebPubSub integration
  • Azure Service Bus integration

Setting Up Azure Resources with Aspire AppHost

1. CosmosDB Integration

// Parent resource
var cosmos = builder.AddAzureCosmosDB("cosmos")
    .RunAsPreviewEmulator();
    
// Child resources
var database = cosmos.AddCosmosDatabase("appointments-app-db");
var container = database.AddContainer("appointments", "/id");

2. Service Bus Integration

// Parent resource
var serviceBus = builder.AddAzureServiceBus("servicebus");

// Child resource
var queue = serviceBus.AddServiceBusQueue("appointment-requests");

3. Web PubSub Integration

// Parent resource
var webpubsub = builder.AddAzureWebPubSub("webpubsub");

// Child resource
var hub = webpubsub.AddHub("apptnotifications");

Connecting Services to Resources

1. Reference-Based Resource Injection

builder.AddProject<Projects.AppointmentProcessor>("appointment-processor")
    .WithReference(database)
    .WithReference(queue)
    .WithReference(hub);

2. Role-Based Access Control

We should make a note that when used in conjunction with the new role-based access control APIs, the role assignments can only be applied on the parent resource. We don't yet support granular assignments on child resources.

In the example below, to set write access on the ServiceBus hub we apply the role assignment to its parent (servcieBus).

// Assigning service-specific roles
builder.AddProject<Projects.Api>("api")
    .WithReference(container)
    .WithReference(queue)
    .WithReference(hub)
    .WithRoleAssignments(webpubsub, WebPubSubBuiltInRole.WebPubSubContributor)
    .WithRoleAssignments(serviceBus, ServiceBusBuiltInRole.AzureServiceBusDataSender);

Client Integration in Services

1. Service Bus Client Integration

For Service Bus, there are no new APIs on the client integration. However, it is possible to WithReference a child resource then use the connection string connfiguration injected by the child resource reference. In the example below, we use the appointment-requests's resouce connection string to initialize the ServiceBus client.

// In Program.cs of a service
builder.AddAzureServiceBusClient("appointment-requests");

// Using the client in an endpoint
app.MapPost("/appointments", async (AppointmentRequest request, 
    ServiceBusClient client) => {
    // Send message to queue
});

2. CosmosDB Client Integration

The CosmosDB client integration has the following new APIs:

  • AddAzureCosmosContainer
  • AddKeyedAzureCosmosContainer
  • AddAzureCosmosDatabase
  • AddKeyedAzureCosmosDatabase

The AddAzureCosmosContainer and AddKeyedAzureCosmosContainer insert an instance the CosmosDB Container class into the DI container. In the example below, the AddAzureCosmosContainer method injects a Container instance that points to the container referenced in the appointments connection string.

// In Program.cs of a service
builder.AddAzureCosmosContainer("appointments");

// Using container in an endpoint
app.MapGet("/appointments", async (Container container) => {
    // Query data from container
});

The AddAzureCosmosDatabase and AddKeyedAzureCosmosDatabase overloads returns an instance of the new CosmosDatabaseBuilder API that can be used to attach multiple containers that are all part of the same database.

In the example below, we can inject Container instances associated with two different containers ("appointments" and "clients") in the same database.

// In Program.cs of a service
builder.AddAzureCosmosDatabase("appointments-app-db")
  .AddKeyedContainer("appointments")
  .AddKeyedContainer("clients");

// Using container in an endpoint
app.MapGet("/appointments", async (
    [FromKeyedServices("appointments")] Container container,
    [FromKeyedServices("clients")] Container container) => {
    // Query data from container
});

Some things to note about the CosmosDatabaseBuilder pattern:

  • In the scenario above, only the database (appointments-app-db) needs to be included as a reference in the AppHost. The appointments and clients strings in the above example refer to container names not database names.
  • All containers parented to the same AddAzureCosmosDatabase call share the same CosmosClient instance. This strategy can be used to associate the same CosmosClientOptions with multiple containers.

3. Web PubSub Client Integration

The WebPubSub integration supports two new overloads:

  • AddKeyedAzureWebPubSubServiceClient(string connectionName)
  • AddAzureWebPubSubServiceClient(string connectionName)

The connectionName that the overloads consume is the connection name associated with a child resource reference in the AppHost. In the example below, apptnotifications is the name of a WebPubSub Hub that has been referenced.

// In Program.cs of a service
builder.AddAzureWebPubSubServiceClient("apptnotifications");

// Using client to broadcast notifications
app.MapPost("/notify", async (WebPubSubServiceClient client, string message) => {
    await client.SendToGroupAsync("appointments", message);
});

The keyed variants of the overload are helpful when multiple hubs need to be referenced from the same application.

// In Program.cs of a service
builder.AddKeyedAzureWebPubSubServiceClient("apptnotifications");
builder.AddKeyedAzureWebPubSubServiceClient("chat");

// Using client to broadcast notifications
app.MapPost("/notify", async ([FromKeyedServices("apptnotifications")] WebPubSubServiceClient client, string message) => {
    await client.SendToGroupAsync("appointments", message);
});

// Using client to broadcast chat messages
app.MapPost("/notify", async ([FromKeyedServices("chat")] WebPubSubServiceClient client, string message) => {
    await client.SendToGroupAsync("message", message);
});

Relevant searches

No response


Associated WorkItem - 416512

@captainsafia captainsafia added the doc-idea Indicates issues that are suggestions for new topics [org][type][category] label Mar 31, 2025
@captainsafia captainsafia added this to the 9.2 milestone Mar 31, 2025
@captainsafia
Copy link
Member Author

@IEvangelist Outline of the new child resource client integration for you.

You can find a sample app showcasing the feature at https://github.com/captainsafia/aspire-child-resources if you want to poke around with it.

I think we can update the integration page for each resource individually with a new heading for the child integration bits.

Let me know if you need any more info.

@IEvangelist
Copy link
Member

IEvangelist commented Apr 1, 2025

@captainsafia — please send me a request for perspective! This is awesome, thank you so much. I cannot overstate how hopeful this is to delivering valuable developer content. 🥳

@IEvangelist IEvangelist self-assigned this Apr 1, 2025
@IEvangelist IEvangelist added 🗺️ reQUEST Triggers an issue to be imported into Quest. area-docs labels Apr 1, 2025
@dotnetrepoman dotnetrepoman bot added the 🗺️ mapQUEST Only used as a way to mark an issue as updated. RepoMan should instantly remove it. label Apr 1, 2025
IEvangelist added a commit to IEvangelist/docs-aspire that referenced this issue Apr 1, 2025
@sequestor sequestor bot added 📌 seQUESTered Identifies that an issue has been imported into Quest. and removed 🗺️ reQUEST Triggers an issue to be imported into Quest. labels Apr 1, 2025
@IEvangelist IEvangelist moved this from 🔖 Ready to 👀 In review in dotnet/docs April 2025 sprint project Apr 8, 2025
@adegeo adegeo removed the 🗺️ mapQUEST Only used as a way to mark an issue as updated. RepoMan should instantly remove it. label Apr 9, 2025
IEvangelist added a commit that referenced this issue Apr 10, 2025
* Add CosmosDB parent-child relationship bits. Part of #2907

* A few fixes

* Update docs/database/azure-cosmos-db-integration.md

Co-authored-by: Safia Abdalla <[email protected]>

* Update docs/database/azure-cosmos-db-integration.md

Co-authored-by: Safia Abdalla <[email protected]>

* A few updates based on feedback

* Add a few more bits

* Add more details about the web pubsub.

* Apply suggestions from code review

Co-authored-by: Safia Abdalla <[email protected]>

---------

Co-authored-by: Safia Abdalla <[email protected]>
@github-project-automation github-project-automation bot moved this from 👀 In review to ✅ Done in dotnet/docs April 2025 sprint project Apr 10, 2025
IEvangelist added a commit that referenced this issue Apr 10, 2025
* Remove IsAspireHost bits. Fixes #2847

* New dashboard config to disable resource graph (#2874)

* Fixes #2842

* Update configuration.md

Co-authored-by: James Newton-King <[email protected]>

---------

Co-authored-by: James Newton-King <[email protected]>

* Position VS Code first

* Add clarifying details about updates to `AddDatabase` APIs (#2878)

* Contributes to #2789

* Added PostgreSQL and links to eventing

* Quick edit pass

* Apply suggestions from code review

Co-authored-by: Andy (Steve) De George <[email protected]>

---------

Co-authored-by: Andy (Steve) De George <[email protected]>

* Adding GitHub Copilot prompts (#2896)

* Initial prompts

* Update prompts

* Adding content for `WithHttpCommand` (#2875)

* Initial setup

* Rework HTTP command content with an example

* Add images and more details.

* Edit pass

* Added playground sample

* Update HTTP command content with updated source

* Remove duplicated code snippet

* Correct member over parameter

* Fix name

* Correct postgres connection command

* Update kafka-integration.md (#2712)

Change links von provectuslabs to Kafbat

* Add role assignments with .NET Aspire (#2891)

* Getting closer on adding roles details

* Apply suggestions from code review

Co-authored-by: Andy (Steve) De George <[email protected]>

* Address feedback and update content

* Add to TOC

* Edit pass

* Add link

* Fix a few issues

* Remove absolute URL

---------

Co-authored-by: Andy (Steve) De George <[email protected]>

* Breaking changes for .NET Aspire 9.2 (#2892)

* Added breaking changes for 9.2, fixes #2888 and fixes #2889

* Correct TOC

* Add clarifying type name

* Fixes #2899

* Added new breaking changes docs

* Remove link

* Correct MD lint error/warnings

* Bump bicep bits (#2930)

* Update docs to use `EndpointProperty.HostAndPort` where appropriate (#2934)

Fixes #2646

* Reorganize the order of the Azure storage content (#2937)

* Fixes #2935

* Tweak table style

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

---------

Co-authored-by: Genevieve Warren <[email protected]>

* Add content on customizing resource URLs (#2940)

* Fixes #2936

* Correct TOC

* Minor tweaks

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

* Apply suggestions from code review

Co-authored-by: Damian Edwards <[email protected]>

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

* Apply suggestions from code review

---------

Co-authored-by: Genevieve Warren <[email protected]>
Co-authored-by: Damian Edwards <[email protected]>

* Add CosmosDB parent-child relationship bits. (#2912)

* Add CosmosDB parent-child relationship bits. Part of #2907

* A few fixes

* Update docs/database/azure-cosmos-db-integration.md

Co-authored-by: Safia Abdalla <[email protected]>

* Update docs/database/azure-cosmos-db-integration.md

Co-authored-by: Safia Abdalla <[email protected]>

* A few updates based on feedback

* Add a few more bits

* Add more details about the web pubsub.

* Apply suggestions from code review

Co-authored-by: Safia Abdalla <[email protected]>

---------

Co-authored-by: Safia Abdalla <[email protected]>

* Add new Azure PostgreSQL client integrations. (#2905)

* Replace manual identity code with new package. Fixes #2883

* Add xref reminders.

* Replace standard clients

* Correct leading para

* Apply suggestions from code review

* Adjustment from peer reviews

* Correct terminology

* .NET Aspire 💜 Playwright (#2904)

* WIP

* Initial bits in place

* Yeah, now we're cookin'!

* OMG, I'm having too much fun!

* Add a few more bits

* Encapsulate selector.

* Even more images automated, and better coverage.

* More coverage...

* Added more coverage.

* More images and coverage.

* A bit more clean up

* Added a few more bits and updated.

* Fixed link issue

* Fix last issue

* Tracing and structured log pages

* Apply suggestions from code review

* Update docs/fundamentals/dashboard/automation/aspire-dashboard/Aspire.Dashboard.ScreenCapture/README.md

* Add "Configure Azure Container App environments" article (#3058)

* Yuck, too rough of a draft but need to context switch.

* Tweaks

* Let's go, this fixes #2938

* Apply suggestions from code review

Co-authored-by: Genevieve Warren <[email protected]>

---------

Co-authored-by: Genevieve Warren <[email protected]>

* What's new in .NET Aspire 9.2 (#2877)

* Add initial bits for #2869. What's new in .NET Aspire 9.2

* Add a few more what's new bits

* Add dashboard config

* Add initial details about HTTP commands

* Apply suggestions from code review

Co-authored-by: Andy (Steve) De George <[email protected]>

* Edit pass

* Add link to breaking changes

* Removed MS-collected telemetry

* Try toying with TOC

* Expand product updates

* Apply suggestions from code review

Co-authored-by: Maddy Montaquila (Leger) <[email protected]>

* Delete section

* Update .NET Aspire 9.2 documentation

* Update docs/whats-new/dotnet-aspire-9.2.md

* Update connection string and deployment section

* Added testing updates

* Update docs/whats-new/dotnet-aspire-9.2.md

Co-authored-by: James Newton-King <[email protected]>

* Clean up

* Minor updates

* Apply suggestions from code review

Co-authored-by: David Fowler <[email protected]>

* Apply suggestions from code review

Co-authored-by: David Fowler <[email protected]>

* Apply suggestions from code review

Co-authored-by: David Fowler <[email protected]>

* Apply suggestions from code review

Co-authored-by: David Fowler <[email protected]>

* Add images

* Final tweaks before release branch

---------

Co-authored-by: Andy (Steve) De George <[email protected]>
Co-authored-by: Maddy Montaquila (Leger) <[email protected]>
Co-authored-by: David Fowler <[email protected]>
Co-authored-by: Mitch Denny <[email protected]>
Co-authored-by: James Newton-King <[email protected]>

* Bump versions

* nits (#3064)

* Fix build issues

* More fixes

* Move things

* Move even more things

* Point to 9.2

* Fix bookmakrs

---------

Co-authored-by: James Newton-King <[email protected]>
Co-authored-by: Andy (Steve) De George <[email protected]>
Co-authored-by: Stefan Nikolei <[email protected]>
Co-authored-by: Alex Crome <[email protected]>
Co-authored-by: Genevieve Warren <[email protected]>
Co-authored-by: Damian Edwards <[email protected]>
Co-authored-by: Safia Abdalla <[email protected]>
Co-authored-by: Maddy Montaquila (Leger) <[email protected]>
Co-authored-by: David Fowler <[email protected]>
Co-authored-by: Mitch Denny <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-docs doc-idea Indicates issues that are suggestions for new topics [org][type][category] 📌 seQUESTered Identifies that an issue has been imported into Quest.
Projects
Development

Successfully merging a pull request may close this issue.

3 participants