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
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ jobs:
shell: bash
run: |
install_path="$RUNNER_TEMP/aspire-cli"
curl -sSL https://aspire.dev/install.sh | bash -s -- --install-path "$install_path" --skip-path
curl -sSL https://aspire.dev/install.sh | bash -s -- --install-path "$install_path" --skip-path --quality staging
echo "$install_path" >> "$GITHUB_PATH"
echo "ASPIRE_CLI=$install_path/aspire" >> "$GITHUB_ENV"
"$install_path/aspire" --version
Expand All @@ -78,7 +78,7 @@ jobs:
$aspirePath = Join-Path $installPath 'aspire.exe'

Invoke-RestMethod 'https://aspire.dev/install.ps1' -OutFile $installScript
& $installScript -InstallPath $installPath -SkipPath
& $installScript -InstallPath $installPath -SkipPath -Quality staging

$installPath | Out-File -FilePath $env:GITHUB_PATH -Encoding utf8 -Append
"ASPIRE_CLI=$aspirePath" | Out-File -FilePath $env:GITHUB_ENV -Encoding utf8 -Append
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Samples for [Aspire](https://aspire.dev).
| Sample | Workload languages | AppHost | Deploy | Description |
| --- | --- | --- | --- | --- |
| [Integrating a Go App](./samples/container-build) | Go | C# AppHost | Docker Compose | Builds and runs a Go Gin app from a Dockerfile with Aspire. |
| [Custom MailDev and MailKit integrations](./samples/maildev-mailkit) | C#, TypeScript | TypeScript AppHost | Run only | Authors custom hosting and client integrations with ATS-generated TypeScript APIs. |
| [Go API](./samples/golang-api) | Go | TypeScript AppHost | Docker Compose | Go + chi API with Aspire-managed run and publish flows. |
| [Python FastAPI + PostgreSQL](./samples/python-fastapi-postgres) | Python | TypeScript AppHost | Docker Compose | FastAPI CRUD API wired to PostgreSQL and pgAdmin. |
| [Python OpenAI Agent](./samples/python-openai-agent) | Python | TypeScript AppHost | Docker Compose | FastAPI AI agent sample with OpenAI integration. |
Expand Down
3 changes: 3 additions & 0 deletions samples/maildev-mailkit/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.aspire/
**/bin/
**/obj/
18 changes: 18 additions & 0 deletions samples/maildev-mailkit/CSharpAppHost/CSharpAppHost.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="13.5.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MailDev.Hosting\MailDev.Hosting.csproj" />
</ItemGroup>

</Project>
13 changes: 13 additions & 0 deletions samples/maildev-mailkit/CSharpAppHost/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Aspire.Hosting;

var builder = DistributedApplication.CreateBuilder(args);

var maildev = builder.AddMailDev("maildev");

builder.AddProject("newsletterservice", "../NewsletterService/NewsletterService.csproj")
.WithHttpHealthCheck("/health")
.WithExternalHttpEndpoints()
.WithReference(maildev)
.WaitFor(maildev);

builder.Build().Run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit.v3" Version="3.2.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MailDev.Hosting\MailDev.Hosting.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using Aspire.Hosting;
using Aspire.Hosting.ApplicationModel;
using System.Reflection;
using Xunit;

namespace MailDev.Hosting.Tests;

public sealed class MailDevResourceTests
{
[Fact]
public void MailDevHostingSurfaceIsExportedForAts()
{
var method = typeof(MailDevResourceBuilderExtensions).GetMethod(
nameof(MailDevResourceBuilderExtensions.AddMailDev),
BindingFlags.Public | BindingFlags.Static);

Assert.NotNull(method);
Assert.Contains(
method.GetCustomAttributes(),
attribute => attribute.GetType().Name == "AspireExportAttribute");
Assert.Contains(
typeof(MailDevResource).GetCustomAttributes(),
attribute => attribute.GetType().Name == "AspireExportAttribute");

var nameParameter = Assert.Single(
method.GetParameters(),
parameter => parameter.Name == "name");
Assert.Contains(
nameParameter.GetCustomAttributes(),
attribute => attribute.GetType().Name == "ResourceNameAttribute");
}

[Fact]
public void AddMailDevConfiguresContainerAndEndpoints()
{
var builder = DistributedApplication.CreateBuilder();

var maildev = builder.AddMailDev("maildev");

var image = Assert.Single(maildev.Resource.Annotations.OfType<ContainerImageAnnotation>());
Assert.Equal("docker.io", image.Registry);
Assert.Equal("maildev/maildev", image.Image);
Assert.Equal("2.2.1", image.Tag);

var endpoints = maildev.Resource.Annotations.OfType<EndpointAnnotation>().ToArray();
var http = Assert.Single(endpoints, endpoint => endpoint.Name == "http");
var smtp = Assert.Single(endpoints, endpoint => endpoint.Name == "smtp");
Assert.Equal(1080, http.TargetPort);
Assert.Equal(1025, smtp.TargetPort);
}

[Fact]
public void AddMailDevCreatesSecretPasswordAndDeferredConnectionString()
{
var builder = DistributedApplication.CreateBuilder();

var maildev = builder.AddMailDev("maildev");

Assert.True(maildev.Resource.PasswordParameter.Secret);
Assert.Null(maildev.Resource.UsernameParameter);
Assert.Equal(
"Endpoint=smtp://{maildev.bindings.smtp.host}:{maildev.bindings.smtp.port};Username=mail-dev;Password={maildev-password.value}",
maildev.Resource.ConnectionStringExpression.ValueExpression);
}

[Fact]
public void AddMailDevUsesProvidedCredentialParameters()
{
var builder = DistributedApplication.CreateBuilder();
var username = builder.AddParameter("smtp-user");
var password = builder.AddParameter("smtp-password", secret: true);

var maildev = builder.AddMailDev(
"maildev",
username: username,
password: password);

Assert.Same(username.Resource, maildev.Resource.UsernameParameter);
Assert.Same(password.Resource, maildev.Resource.PasswordParameter);
Assert.Equal(
"Endpoint=smtp://{maildev.bindings.smtp.host}:{maildev.bindings.smtp.port};Username={smtp-user.value};Password={smtp-password.value}",
maildev.Resource.ConnectionStringExpression.ValueExpression);
}
}
14 changes: 14 additions & 0 deletions samples/maildev-mailkit/MailDev.Hosting/MailDev.Hosting.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<EnableAspireIntegrationAnalyzers>true</EnableAspireIntegrationAnalyzers>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These XML comments do not reach the generated ATS SDK because this project does not emit MailDev.Hosting.xml (confirmed in the build output). Could we enable GenerateDocumentationFile and inspect or assert the generated .d.ts JSDoc?

</PropertyGroup>

<ItemGroup>
<PackageReference Include="Aspire.Hosting" Version="13.5.0" />
</ItemGroup>

</Project>
36 changes: 36 additions & 0 deletions samples/maildev-mailkit/MailDev.Hosting/MailDevResource.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
namespace Aspire.Hosting.ApplicationModel;

/// <summary>Represents a MailDev container resource.</summary>
[AspireExport]
public sealed class MailDevResource(
[ResourceName] string name,
ParameterResource? username,
ParameterResource password)
: ContainerResource(name), IResourceWithConnectionString
{
internal const string HttpEndpointName = "http";
internal const string SmtpEndpointName = "smtp";

private const string DefaultUsername = "mail-dev";
private EndpointReference? _smtpEndpoint;

/// <summary>Gets the optional MailDev SMTP username parameter.</summary>
public ParameterResource? UsernameParameter { get; } = username;

/// <summary>Gets the MailDev SMTP password parameter.</summary>
public ParameterResource PasswordParameter { get; } = password;

internal ReferenceExpression UsernameReference =>
UsernameParameter is not null
? ReferenceExpression.Create($"{UsernameParameter}")
: ReferenceExpression.Create($"{DefaultUsername}");

/// <summary>Gets the MailDev SMTP endpoint.</summary>
public EndpointReference SmtpEndpoint =>
_smtpEndpoint ??= new(this, SmtpEndpointName);

/// <inheritdoc />
public ReferenceExpression ConnectionStringExpression =>
ReferenceExpression.Create(
$"Endpoint=smtp://{SmtpEndpoint.Property(EndpointProperty.HostAndPort)};Username={UsernameReference};Password={PasswordParameter}");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this is an integration-authoring sample, could we implement GetConnectionProperties() for Host, Port, Username, Password, and Uri? The current default is empty, so generated/polyglot consumers only see the opaque connection string and miss the structured pattern we are trying to teach.

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using Aspire.Hosting.ApplicationModel;

namespace Aspire.Hosting;

/// <summary>Provides extension methods for adding MailDev resources.</summary>
public static class MailDevResourceBuilderExtensions
{
private const string Registry = "docker.io";
private const string Image = "maildev/maildev";
private const string Tag = "2.2.1";
private const string UsernameEnvironmentVariable = "MAILDEV_INCOMING_USER";
private const string PasswordEnvironmentVariable = "MAILDEV_INCOMING_PASS";

/// <summary>Adds a MailDev container resource.</summary>
/// <param name="builder">The distributed application builder.</param>
/// <param name="name">The resource name.</param>
/// <param name="httpPort">The optional host port for the MailDev web interface.</param>
/// <param name="smtpPort">The optional host port for SMTP.</param>
/// <param name="username">The optional SMTP username parameter.</param>
/// <param name="password">The optional SMTP password parameter.</param>
/// <returns>The MailDev resource builder.</returns>
[AspireExport]
public static IResourceBuilder<MailDevResource> AddMailDev(
this IDistributedApplicationBuilder builder,
[ResourceName] string name,
int? httpPort = null,
int? smtpPort = null,
IResourceBuilder<ParameterResource>? username = null,
IResourceBuilder<ParameterResource>? password = null)
{
ArgumentNullException.ThrowIfNull(builder);

var passwordParameter = password?.Resource ??
ParameterResourceBuilderExtensions.CreateDefaultPasswordParameter(
builder, $"{name}-password");
var resource = new MailDevResource(name, username?.Resource, passwordParameter);

return builder.AddResource(resource)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Since this sample is marked run-only, could we exclude MailDev from the manifest? I confirmed the pinned head publishes it as container.v0 with its SMTP credential parameter, which teaches the wrong lifecycle for a development mail sink. .ExcludeFromManifest() plus a manifest assertion should cover it.

.WithImage(Image)
.WithImageRegistry(Registry)
.WithImageTag(Tag)
.WithHttpEndpoint(
targetPort: 1080,
port: httpPort,
name: MailDevResource.HttpEndpointName)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

WaitFor(maildev) only waits for the container to reach Running here because the resource has no health check. MailDev exposes /healthz; could we add an HTTP health check so the newsletter service waits for actual readiness rather than racing SMTP startup?

.WithEndpoint(
targetPort: 1025,
port: smtpPort,
name: MailDevResource.SmtpEndpointName)
.WithEnvironment(context =>
{
context.EnvironmentVariables[UsernameEnvironmentVariable] = resource.UsernameReference;
context.EnvironmentVariables[PasswordEnvironmentVariable] = resource.PasswordParameter;
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.3.0" />
<PackageReference Include="xunit.v3" Version="3.2.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.1.5" PrivateAssets="all" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\MailKit.Client\MailKit.Client.csproj" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using MailKit.Client;
using Xunit;

namespace MailKit.Client.Tests;

public sealed class MailKitClientSettingsTests
{
[Fact]
public void ParseConnectionStringAcceptsUri()
{
var settings = new MailKitClientSettings();

settings.ParseConnectionString("smtp://localhost:1025");

Assert.Equal(new Uri("smtp://localhost:1025"), settings.Endpoint);
Assert.Null(settings.Credentials);
}

[Fact]
public void ParseConnectionStringAcceptsEndpointAndCredentials()
{
var settings = new MailKitClientSettings();

settings.ParseConnectionString(
"Endpoint=smtp://localhost:1025;Username=mail-dev;Password=secret");

Assert.Equal(new Uri("smtp://localhost:1025"), settings.Endpoint);
Assert.Equal("mail-dev", settings.Credentials?.UserName);
Assert.Equal("secret", settings.Credentials?.Password);
}

[Theory]
[InlineData(null)]
[InlineData("")]
[InlineData("Host=localhost")]
public void ParseConnectionStringRejectsMissingEndpoint(string? connectionString)
{
var settings = new MailKitClientSettings();

Assert.Throws<InvalidOperationException>(
() => settings.ParseConnectionString(connectionString));
}
}
Loading
Loading