Skip to content
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
20 changes: 20 additions & 0 deletions src/Aspire.Hosting/ApplicationModel/EnvironmentAnnotation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Aspire.Hosting.ApplicationModel;

// The purpose of this type is to improve the debugging experience when inspecting environment variables set without callback.
[DebuggerDisplay("Type = {GetType().Name,nq}, Name = {_name}, Value = {_value}")]
internal class EnvironmentAnnotation : EnvironmentCallbackAnnotation
{
private readonly string _name;
private readonly string _value;

public EnvironmentAnnotation(string name, string value) : base(name, () => value)
{
_name = name;
_value = value;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Diagnostics;

namespace Aspire.Hosting.ApplicationModel;

/// <summary>
/// Represents an annotation that provides a callback to modify the environment variables of an application.
/// </summary>
[DebuggerDisplay("{DebuggerToString(),nq}")]
public class EnvironmentCallbackAnnotation : IResourceAnnotation
{
private readonly string? _name;

/// <summary>
/// Initializes a new instance of the <see cref="EnvironmentCallbackAnnotation"/> class with the specified name and callback function.
/// </summary>
/// <param name="name">The name of the environment variable to set.</param>
/// <param name="callback">The callback function that returns the value to set the environment variable to.</param>
public EnvironmentCallbackAnnotation(string name, Func<string> callback)
{
_name = name;
Callback = (c) => c.EnvironmentVariables[name] = callback();
}

Expand All @@ -40,4 +46,14 @@ public EnvironmentCallbackAnnotation(Action<EnvironmentCallbackContext> callback
/// Gets or sets the callback action to be executed when the environment is being built.
/// </summary>
public Action<EnvironmentCallbackContext> Callback { get; private set; }

private string DebuggerToString()
{
var text = $@"Type = {GetType().Name}";
if (_name != null)
{
text += $@", Name = ""{_name}""";
}
return text;
}
}
7 changes: 2 additions & 5 deletions src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public static class ResourceBuilderExtensions
/// <returns>A resource configured with the specified environment variable.</returns>
public static IResourceBuilder<T> WithEnvironment<T>(this IResourceBuilder<T> builder, string name, string? value) where T : IResource
{
return builder.WithAnnotation(new EnvironmentCallbackAnnotation(name, () => value ?? string.Empty));
return builder.WithAnnotation(new EnvironmentAnnotation(name, value ?? string.Empty));
}

/// <summary>
Expand Down Expand Up @@ -222,10 +222,7 @@ public static IResourceBuilder<TDestination> WithReference<TDestination>(this IR
throw new InvalidOperationException("The uri absolute path must be \"/\".");
}

return builder.WithEnvironment(context =>
{
context.EnvironmentVariables[$"services__{name}"] = uri.ToString();
});
return builder.WithEnvironment($"services__{name}", uri.ToString());
}

/// <summary>
Expand Down