microsoft/aspire · error · InvalidOperationException

Value has already been set.

Error message

Value has already been set.

What it means

This is a write-once guard on a value provider: Set(T) throws InvalidOperationException if the value has already been assigned. The Foundry hosted agent value providers are designed so each provider's value is set exactly once, typically during provisioning/configuration resolution.

Solutions

  1. Call Set at most once per provider instance
  2. Create a new provider instance if you need to set a value again
  3. Restructure code so the value is computed once before being assigned
  4. In tests, use a fresh provider per test case

Example fix

// before
var provider = new HostedAgentValueProvider<string>();
provider.Set("a");
provider.Set("b"); // throws
// after
var provider = new HostedAgentValueProvider<string>();
provider.Set("a");
var provider2 = new HostedAgentValueProvider<string>();
provider2.Set("b");
Defensive patterns

Strategy: try-catch

Validate before calling

// No pre-check API exists; track assignment yourself
bool assigned = false;
void Assign(Provider p, string v) { if (assigned) throw new InvalidOperationException("Already assigned"); p.Set(v); assigned = true; }

Try / catch

try
{
    provider.Set(value);
}
catch (InvalidOperationException ex) when (ex.Message == "Value has already been set.")
{
    logger.LogWarning("Provider value was set more than once; ignoring duplicate set.");
}

Prevention

When it happens

Trigger: Calling Set twice on the same provider instance — e.g. reusing a provider across two provisioning runs, or calling Set explicitly after the framework already set the value.

Common situations: Unit tests setting a value then setting it again; app startup logic that re-runs configuration and re-invokes Set on a singleton provider; accidentally sharing one provider instance between two resources.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of microsoft/aspire@25830f84bd (2026-09-16). Data as JSON: /api/errors/177a300f51d40f92. Report an issue: GitHub.

Appendix: source

Thrown at src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs:524

/// A static value provider that returns a fixed value once it's been set.
/// </summary>

public class StaticValueProvider<T> : IValueProvider, IManifestExpressionProvider
{
    private T? _value;
    private bool _isSet;

    /// <inheritdoc/>
    public string ValueExpression => "{value}";

    /// <summary>
    /// Sets the value of the provider.
    /// </summary>
    public void Set(T value)
    {
        if (_isSet)
        {
            throw new InvalidOperationException($"Value has already been set.");
        }
        _value = value;
        _isSet = true;
    }

    /// <summary>
    /// Creates a new instance of the <see cref="StaticValueProvider{T}"/> class.
    /// </summary>
    public StaticValueProvider()
    {
        _isSet = false;
    }

    /// <summary>
    /// Creates a new instance of the <see cref="StaticValueProvider{T}"/> class.
    /// </summary>
    public StaticValueProvider(T value)
    {

View on GitHub (pinned to 25830f84bd)