Skip to content

feat: Add MongoDB hosting extension with dashboard management commands#87

Merged
mpaulosky merged 5 commits intomainfrom
copilot/add-mongodb-extension
Feb 17, 2026
Merged

feat: Add MongoDB hosting extension with dashboard management commands#87
mpaulosky merged 5 commits intomainfrom
copilot/add-mongodb-extension

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Feb 17, 2026

Summary

AppHost needed reusable MongoDB registration with dashboard commands for data management during development. Current setup requires verbose chaining; no built-in data reset capabilities.

Changes

Core Extension (src/AppHost/Extensions/MongoDBHostingExtensions.cs)

  • AddMongoDBWithManagement() method encapsulates MongoDB registration, health checks, and data volume configuration
  • Two dashboard commands using Aspire 13.0 CommandOptions API:
    • Clear All Data: Drops all collections via ListCollectionNamesAsync() + DropCollectionAsync()
    • Drop Database: Drops entire database with confirmation; uses DropDatabaseAsync()
  • Exception handling for MongoException and OperationCanceledException

AppHost Updates

Simplified registration from:

var mongodb = builder.AddMongoDB("mongodb")
    .WithDataVolume()
    .WithHealthCheck("mongodb");

To:

var mongodb = builder.AddMongoDBWithManagement("mongodb");

Maintains identical functionality with added dashboard commands.

Testing

  • New AppHost.Tests project with 12 unit tests
  • Coverage: resource creation, parameter validation, annotation verification
  • All tests passing

Documentation

  • README.md in Extensions directory with usage, security considerations, and API reference

Technical Notes

  • Uses IAsyncCursor<string> iteration pattern for MongoDB collection enumeration
  • Dashboard commands execute async; state management via CommandOptions.UpdateState
  • Commands always enabled; confirmation handled by Aspire dashboard UI
Original prompt

This section details on the original issue you should resolve

<issue_title>Feature: Add to the AppHost an extension

<issue_description>In the AppHost lets create an extension that:

  • Registers the MongoDB database.
  • Creates a Dashboard command that allow clearing the data or dropping the database to allow add new data.</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 4 commits February 17, 2026 18:28
Co-authored-by: mpaulosky <60372079+mpaulosky@users.noreply.github.com>
Co-authored-by: mpaulosky <60372079+mpaulosky@users.noreply.github.com>
Co-authored-by: mpaulosky <60372079+mpaulosky@users.noreply.github.com>
Co-authored-by: mpaulosky <60372079+mpaulosky@users.noreply.github.com>
Copilot AI changed the title [WIP] Add MongoDB extension to AppHost with dashboard command feat: Add MongoDB hosting extension with dashboard management commands Feb 17, 2026
Copilot AI requested a review from mpaulosky February 17, 2026 18:39
@mpaulosky mpaulosky marked this pull request as ready for review February 17, 2026 18:54
Copilot AI review requested due to automatic review settings February 17, 2026 18:54
@mpaulosky mpaulosky merged commit c188707 into main Feb 17, 2026
7 checks passed
@mpaulosky mpaulosky deleted the copilot/add-mongodb-extension branch February 17, 2026 18:55
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request adds a reusable MongoDB hosting extension for the AppHost with integrated dashboard management commands, simplifying MongoDB configuration and providing data management capabilities during development.

Changes:

  • Created MongoDBHostingExtensions class with AddMongoDBWithManagement() method that encapsulates MongoDB registration, health checks, and data volumes
  • Added two dashboard commands: "Clear All Data" (drops all collections) and "Drop Database" (drops entire database with confirmation)
  • Added comprehensive unit tests in new AppHost.Tests project (12 tests covering resource creation, validation, and annotations)
  • Simplified AppHost Program.cs by replacing verbose chaining with single method call
  • Added extensive documentation in Extensions/README.md covering usage, security, and API reference

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
src/AppHost/Extensions/MongoDBHostingExtensions.cs Core extension implementing MongoDB registration with dashboard commands for clearing data and dropping database
src/AppHost/Extensions/README.md Comprehensive documentation covering features, usage, parameters, exception handling, testing, and security considerations
src/AppHost/Program.cs Simplified MongoDB registration using new extension method
src/AppHost/GlobalUsings.cs Added required global usings for Aspire and MongoDB
src/AppHost/AppHost.csproj Added MongoDB.Driver package reference
tests/AppHost.Tests/MongoDBHostingExtensionsTests.cs Unit tests covering resource creation, parameter validation, and annotation verification
tests/AppHost.Tests/GlobalUsings.cs Global usings for test project
tests/AppHost.Tests/AppHost.Tests.csproj Test project configuration with required dependencies
IssueTracker.slnx Added AppHost.Tests project to solution

Comment on lines +9 to +14

namespace AppHost.Tests;

/// <summary>
/// Tests for MongoDB hosting extensions with dashboard commands.
/// </summary>
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test class is missing the [ExcludeFromCodeCoverage] attribute. All test classes in this repository consistently use this attribute to exclude test code from coverage metrics (see examples in DatabaseSettingsTests.cs, MongoDbContextFactoryTests.cs, etc.). Add [ExcludeFromCodeCoverage] above the class declaration to maintain consistency with the codebase conventions.

Suggested change
namespace AppHost.Tests;
/// <summary>
/// Tests for MongoDB hosting extensions with dashboard commands.
/// </summary>
using System.Diagnostics.CodeAnalysis;
namespace AppHost.Tests;
/// <summary>
/// Tests for MongoDB hosting extensions with dashboard commands.
/// </summary>
[ExcludeFromCodeCoverage]

Copilot uses AI. Check for mistakes.
Comment on lines +69 to +75
var clearedCount = 0;
while (await collections.MoveNextAsync(context.CancellationToken))
{
foreach (var collectionName in collections.Current)
{
await db.DropCollectionAsync(collectionName, context.CancellationToken);
clearedCount++;
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The variable 'clearedCount' is declared and incremented but never used. Either remove this variable if it's not needed, or consider including it in the success result for logging/debugging purposes.

Suggested change
var clearedCount = 0;
while (await collections.MoveNextAsync(context.CancellationToken))
{
foreach (var collectionName in collections.Current)
{
await db.DropCollectionAsync(collectionName, context.CancellationToken);
clearedCount++;
while (await collections.MoveNextAsync(context.CancellationToken))
{
foreach (var collectionName in collections.Current)
{
await db.DropCollectionAsync(collectionName, context.CancellationToken);

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,155 @@
// ============================================
// Copyright (c) 2023. All rights reserved.
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year is 2023, but this is new code being added in 2025-2026. Consider updating to the current year to accurately reflect when this code was created.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,19 @@
// ============================================
// Copyright (c) 2023. All rights reserved.
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year is 2023, but this is new code being added in 2025-2026. Consider updating to the current year to accurately reflect when this code was created.

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,138 @@
// ============================================
// Copyright (c) 2023. All rights reserved.
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The copyright year is 2023, but this is new code being added in 2025-2026. Consider updating to the current year to accurately reflect when this code was created.

Copilot uses AI. Check for mistakes.

try
{
var client = new MongoClient(connectionString);
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MongoClient instance created in line 65 is not disposed. MongoClient is an IDisposable resource that should be properly disposed. Consider using a 'using' statement or ensure the client is disposed after use to prevent potential resource leaks.

Suggested change
var client = new MongoClient(connectionString);
using var client = new MongoClient(connectionString);

Copilot uses AI. Check for mistakes.

try
{
var client = new MongoClient(connectionString);
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The MongoClient instance created in line 119 is not disposed. MongoClient is an IDisposable resource that should be properly disposed. Consider using a 'using' statement or ensure the client is disposed after use to prevent potential resource leaks.

Copilot uses AI. Check for mistakes.
var mongodb = builder.AddMongoDBWithManagement("testmongo");

// Assert
mongodb.Resource.Annotations.Should().NotBeEmpty("Resource should have annotations");
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test 'AddMongoDBWithManagement_AddsDataVolumeAnnotation' only asserts that annotations are not empty, but doesn't verify that a data volume annotation specifically exists. Consider adding a more specific assertion to verify the presence of a volume-related annotation, similar to the health check test which checks for a HealthCheck annotation type.

Suggested change
mongodb.Resource.Annotations.Should().NotBeEmpty("Resource should have annotations");
mongodb.Resource.Annotations.Should().NotBeEmpty("Resource should have annotations");
mongodb.Resource.Annotations.Should().Contain(
a => a.GetType().Name.Contains("Volume"),
"Resource should have data volume annotation");

Copilot uses AI. Check for mistakes.
### Requirements

- .NET 10.0
- Aspire.Hosting.MongoDB 13.0+
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The requirements specify "Aspire.Hosting.MongoDB 13.0+", but according to Directory.Packages.props line 54, the project is using "Aspire.Hosting.MongoDB" version "13.0.0". While technically correct, consider verifying this version is available and stable. The PR description mentions using "Aspire 13.0 CommandOptions API", which aligns with this version.

Copilot uses AI. Check for mistakes.
Comment on lines +81 to +89
catch (MongoException)
{
// MongoDB-specific errors (connection, authentication, etc.)
return CommandResults.Failure();
}
catch (OperationCanceledException)
{
// Operation was cancelled by user or timeout
return CommandResults.Failure();
Copy link

Copilot AI Feb 17, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception handlers for MongoException and OperationCanceledException silently return failure without logging the error or providing any diagnostic information. Consider adding logging or including exception details in the failure result to aid in debugging when commands fail. This would help developers understand why a command failed in the dashboard.

Suggested change
catch (MongoException)
{
// MongoDB-specific errors (connection, authentication, etc.)
return CommandResults.Failure();
}
catch (OperationCanceledException)
{
// Operation was cancelled by user or timeout
return CommandResults.Failure();
catch (MongoException ex)
{
// MongoDB-specific errors (connection, authentication, etc.)
return CommandResults.Failure(
$"MongoDB error while clearing data from '{databaseName}': {ex.Message}");
}
catch (OperationCanceledException)
{
// Operation was cancelled by user or timeout
return CommandResults.Failure(
"Operation was canceled while clearing MongoDB data.");

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

3 participants