diff --git a/src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs b/src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs index a9cb090e97..e35e4b2d27 100644 --- a/src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs +++ b/src/Aspire.Hosting/ApplicationModel/InputAnnotation.cs @@ -18,6 +18,7 @@ public sealed class InputAnnotation : IResourceAnnotation, IValueProvider { private string? _value; private bool _hasValue; + private Func? _valueGetter; /// /// Initializes a new instance of . @@ -45,23 +46,28 @@ public InputAnnotation(string name, bool secret = false) /// public InputDefault? Default { get; set; } - internal Func? ValueGetter { get; set; } - - ValueTask IValueProvider.GetValueAsync(CancellationToken cancellationToken) + internal string? Value { - if (!_hasValue) + get { - _value = GenerateValue(); - _hasValue = true; + if (!_hasValue) + { + _value = GenerateValue(); + _hasValue = true; + } + return _value; } - return new(_value); } + ValueTask IValueProvider.GetValueAsync(CancellationToken cancellationToken) => new(Value); + + internal void SetValueGetter(Func valueGetter) => _valueGetter = valueGetter; + private string GenerateValue() { - if (ValueGetter is not null) + if (_valueGetter is not null) { - return ValueGetter(); + return _valueGetter(); } if (Default is null) diff --git a/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs b/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs index 1d99a9b367..88e7ed25e1 100644 --- a/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs @@ -19,7 +19,8 @@ public sealed class ParameterResource : Resource, IManifestExpressionProvider, I public ParameterResource(string name, Func callback, bool secret = false) : base(name) { _valueInput = new InputAnnotation("value", secret); - _valueInput.ValueGetter = callback; + _valueInput.SetValueGetter(callback); + Annotations.Add(_valueInput); ValueInputReference = new InputReference(this, "value"); @@ -28,7 +29,7 @@ public ParameterResource(string name, Func callback, bool secret = false /// /// Gets the value of the parameter. /// - public string Value => _valueInput.ValueGetter!(); + public string Value => _valueInput.Value ?? throw new InvalidOperationException("A Parameter's value cannot be null."); /// /// Gets a value indicating whether the parameter is secret.