diff --git a/src/Aspire.Hosting/ApplicationModel/EnvironmentAnnotation.cs b/src/Aspire.Hosting/ApplicationModel/EnvironmentAnnotation.cs new file mode 100644 index 00000000000..29444de3856 --- /dev/null +++ b/src/Aspire.Hosting/ApplicationModel/EnvironmentAnnotation.cs @@ -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; + } +} diff --git a/src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackAnnotation.cs b/src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackAnnotation.cs index 7a5190a119e..d52e34428eb 100644 --- a/src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackAnnotation.cs +++ b/src/Aspire.Hosting/ApplicationModel/EnvironmentCallbackAnnotation.cs @@ -1,13 +1,18 @@ // 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; /// /// Represents an annotation that provides a callback to modify the environment variables of an application. /// +[DebuggerDisplay("{DebuggerToString(),nq}")] public class EnvironmentCallbackAnnotation : IResourceAnnotation { + private readonly string? _name; + /// /// Initializes a new instance of the class with the specified name and callback function. /// @@ -15,6 +20,7 @@ public class EnvironmentCallbackAnnotation : IResourceAnnotation /// The callback function that returns the value to set the environment variable to. public EnvironmentCallbackAnnotation(string name, Func callback) { + _name = name; Callback = (c) => c.EnvironmentVariables[name] = callback(); } @@ -40,4 +46,14 @@ public EnvironmentCallbackAnnotation(Action callback /// Gets or sets the callback action to be executed when the environment is being built. /// public Action Callback { get; private set; } + + private string DebuggerToString() + { + var text = $@"Type = {GetType().Name}"; + if (_name != null) + { + text += $@", Name = ""{_name}"""; + } + return text; + } } diff --git a/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs b/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs index 37033c15890..119701f4193 100644 --- a/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs +++ b/src/Aspire.Hosting/Extensions/ResourceBuilderExtensions.cs @@ -26,7 +26,7 @@ public static class ResourceBuilderExtensions /// A resource configured with the specified environment variable. public static IResourceBuilder WithEnvironment(this IResourceBuilder 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)); } /// @@ -222,10 +222,7 @@ public static IResourceBuilder WithReference(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()); } ///