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

Allow reading connection strings from config #133

Merged
merged 2 commits into from
Oct 9, 2023
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
19 changes: 13 additions & 6 deletions src/Aspire.Hosting/ComponentBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.

using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Configuration;

namespace Aspire.Hosting;

public static class ComponentBuilderExtensions
{
private const string ConnectionStringEnvironmentName = "ConnectionStrings__";

public static AllocatedEndpointAnnotation? GetEndpoint<T>(this IDistributedApplicationComponentBuilder<T> builder, string name) where T : IDistributedApplicationComponent
{
return builder.Component.Annotations.OfType<AllocatedEndpointAnnotation>().SingleOrDefault();
Expand Down Expand Up @@ -68,28 +71,32 @@ private static Action<EnvironmentCallbackContext> CreateServiceReferenceEnvironm
};
}

public static IDistributedApplicationComponentBuilder<TDestination> WithReference<TDestination, TSource>(this IDistributedApplicationComponentBuilder<TDestination> builder, IDistributedApplicationComponentBuilder<TSource> source)
public static IDistributedApplicationComponentBuilder<TDestination> WithReference<TDestination, TSource>(this IDistributedApplicationComponentBuilder<TDestination> builder, IDistributedApplicationComponentBuilder<TSource> source, string? connectionName = null)
where TDestination : IDistributedApplicationComponentWithEnvironment
where TSource : IDistributedApplicationComponentWithConnectionString
{
var connectionName = $"ConnectionStrings__{source.Component.Name}";
var component = source.Component;
connectionName ??= component.Name;

return builder.WithEnvironment(context =>
{
var connectionStringName = $"{ConnectionStringEnvironmentName}{connectionName}";

if (context.PublisherName == "manifest")
{
context.EnvironmentVariables[connectionName] = $"{{{source.Component.Name}.connectionString}}";
context.EnvironmentVariables[connectionStringName] = $"{{{component.Name}.connectionString}}";
return;
}

var connectionString = source.Component.GetConnectionString();
var connectionString = component.GetConnectionString() ??
builder.ApplicationBuilder.Configuration.GetConnectionString(component.Name);

if (string.IsNullOrEmpty(connectionString))
{
throw new DistributedApplicationException($"A connection string for '{source.Component.Name}' could not be retrieved.");
throw new DistributedApplicationException($"A connection string for '{component.Name}' could not be retrieved.");
}

context.EnvironmentVariables[connectionName] = connectionString;
context.EnvironmentVariables[connectionStringName] = connectionString;
});
}

Expand Down
10 changes: 7 additions & 3 deletions src/Aspire.Hosting/Postgres/PostgresBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Sockets;
using System.Text.Json;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Configuration;

namespace Aspire.Hosting.Postgres;

Expand Down Expand Up @@ -32,7 +33,7 @@ public static IDistributedApplicationComponentBuilder<PostgresContainerComponent
});
}

public static IDistributedApplicationComponentBuilder<PostgresComponent> AddPostgres(this IDistributedApplicationBuilder builder, string name, string? connectionString)
public static IDistributedApplicationComponentBuilder<PostgresComponent> AddPostgres(this IDistributedApplicationBuilder builder, string name, string? connectionString = null)
{
var postgres = new PostgresComponent(name, connectionString);

Expand Down Expand Up @@ -60,7 +61,7 @@ public static IDistributedApplicationComponentBuilder<T> WithPostgresDatabase<T>
where T : IDistributedApplicationComponentWithEnvironment
{
var postgres = postgresBuilder.Component;
connectionName = connectionName ?? postgresBuilder.Component.Name;
connectionName ??= postgresBuilder.Component.Name;

return builder.WithEnvironment((context) =>
{
Expand All @@ -72,11 +73,14 @@ public static IDistributedApplicationComponentBuilder<T> WithPostgresDatabase<T>
return;
}

var connectionString = postgres.GetConnectionString(databaseName);
var connectionString = postgres.GetConnectionString(databaseName) ??
builder.ApplicationBuilder.Configuration.GetConnectionString(postgres.Name);

if (string.IsNullOrEmpty(connectionString))
{
throw new DistributedApplicationException($"A connection string for Postgres '{postgres.Name}' could not be retrieved.");
}

context.EnvironmentVariables[connectionStringName] = connectionString;
});
}
Expand Down
4 changes: 2 additions & 2 deletions src/Aspire.Hosting/Redis/RedisBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public static IDistributedApplicationComponentBuilder<RedisContainerComponent> A
return componentBuilder;
}

public static IDistributedApplicationComponentBuilder<RedisComponent> AddRedis(this IDistributedApplicationBuilder builder, string name, string? connectionString)
public static IDistributedApplicationComponentBuilder<RedisComponent> AddRedis(this IDistributedApplicationBuilder builder, string name, string? connectionString = null)
{
var redis = new RedisComponent(name, connectionString);

Expand All @@ -44,6 +44,6 @@ private static void WriteRedisComponentToManifest(Utf8JsonWriter jsonWriter, str
public static IDistributedApplicationComponentBuilder<T> WithRedis<T>(this IDistributedApplicationComponentBuilder<T> builder, IDistributedApplicationComponentBuilder<IRedisComponent> redisBuilder, string? connectionName = null)
where T : IDistributedApplicationComponentWithEnvironment
{
return builder.WithReference(redisBuilder);
return builder.WithReference(redisBuilder, connectionName);
Copy link
Member Author

Choose a reason for hiding this comment

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

This was a bug I introduced.

}
}
10 changes: 7 additions & 3 deletions src/Aspire.Hosting/SqlServer/SqlServerBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Net.Sockets;
using System.Text.Json;
using Aspire.Hosting.ApplicationModel;
using Microsoft.Extensions.Configuration;

namespace Aspire.Hosting.SqlServer;

Expand All @@ -24,7 +25,7 @@ public static IDistributedApplicationComponentBuilder<SqlServerContainerComponen
return componentBuilder;
}

public static IDistributedApplicationComponentBuilder<SqlServerComponent> AddSqlServer(this IDistributedApplicationBuilder builder, string name, string? connectionString)
public static IDistributedApplicationComponentBuilder<SqlServerComponent> AddSqlServer(this IDistributedApplicationBuilder builder, string name, string? connectionString = null)
{
var sqlServer = new SqlServerComponent(name, connectionString);

Expand All @@ -49,7 +50,7 @@ public static IDistributedApplicationComponentBuilder<T> WithSqlServer<T>(this I
where T : IDistributedApplicationComponentWithEnvironment
{
var sql = sqlBuilder.Component;
connectionName = connectionName ?? sqlBuilder.Component.Name;
connectionName ??= sqlBuilder.Component.Name;

return builder.WithEnvironment((context) =>
{
Expand All @@ -61,11 +62,14 @@ public static IDistributedApplicationComponentBuilder<T> WithSqlServer<T>(this I
return;
}

var connectionString = sql.GetConnectionString(databaseName);
var connectionString = sql.GetConnectionString(databaseName) ??
builder.ApplicationBuilder.Configuration.GetConnectionString(sql.Name);

if (string.IsNullOrEmpty(connectionString))
{
throw new DistributedApplicationException($"A connection string for SqlServer '{sql.Name}' could not be retrieved.");
}

context.EnvironmentVariables[connectionStringName] = connectionString;
});
}
Expand Down