diff --git a/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs b/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs index 662d0d8e780..f2560c6777e 100644 --- a/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs +++ b/src/Aspire.Hosting/ApplicationModel/ParameterResource.cs @@ -8,8 +8,7 @@ namespace Aspire.Hosting.ApplicationModel; /// public class ParameterResource : Resource, IResourceWithoutLifetime, IManifestExpressionProvider, IValueProvider { - private string? _value; - private bool _hasValue; + private readonly Lazy _lazyValue; private readonly Func _valueGetter; private string? _configurationKey; @@ -25,6 +24,7 @@ public ParameterResource(string name, Func callback, ArgumentNullException.ThrowIfNull(callback); _valueGetter = callback; + _lazyValue = new Lazy(() => _valueGetter(Default)); Secret = secret; } @@ -33,18 +33,7 @@ public ParameterResource(string name, Func callback, /// public string Value => GetValueAsync(default).AsTask().GetAwaiter().GetResult()!; - internal string ValueInternal - { - get - { - if (!_hasValue) - { - _value = _valueGetter(Default); - _hasValue = true; - } - return _value!; - } - } + internal string ValueInternal => _lazyValue.Value; /// /// Represents how the default value of the parameter should be retrieved.