{"record":{"id":"56550820b0380276","repo":"microsoft/aspire","slug":"value-for-provider-has-not-been-set","errorCode":null,"errorMessage":"Value for provider has not been set.","messagePattern":"Value for provider has not been set\\.","errorType":"validation","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs","lineNumber":552,"sourceCode":"    {\n        _isSet = false;\n    }\n\n    /// <summary>\n    /// Creates a new instance of the <see cref=\"StaticValueProvider{T}\"/> class.\n    /// </summary>\n    public StaticValueProvider(T value)\n    {\n        _value = value;\n        _isSet = true;\n    }\n\n    /// <inheritdoc/>\n    public ValueTask<string?> GetValueAsync(CancellationToken cancellationToken = default)\n    {\n        if (_isSet == false)\n        {\n            throw new InvalidOperationException(\"Value for provider has not been set.\");\n        }\n        else\n        {\n            return ValueTask.FromResult(_value?.ToString());\n        }\n    }\n}\n","sourceCodeStart":534,"sourceCodeEnd":560,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Foundry/HostedAgent/AzureHostedAgentResource.cs#L534-L560","documentation":"GetValueAsync on a Foundry hosted agent value provider throws InvalidOperationException when Set was never called (_isSet is false). The provider is intentionally write-once/read-after-write, so reading before assignment is a programming error rather than a null return.","triggerScenarios":"Calling GetValueAsync before the framework (or your code) has called Set — e.g. reading the value before provisioning/config resolution completes, or a code path that conditionally skips Set.","commonSituations":"Reading the value too early in the application lifecycle (before the value resolution phase); a conditional branch that fails to call Set; test that reads an unconfigured provider.","solutions":["Ensure Set is called before any GetValueAsync call","Check ordering: wait for the provisioning/configuration phase that assigns provider values before reading","Verify all code paths that create the provider also call Set on it","If unset is legitimate, restructure to use a nullable holder instead of this provider"],"exampleFix":"// before\nvar value = await provider.GetValueAsync(); // throws: never set\n// after\nif (!isConfigured) provider.Set(defaultValue);\nvar value = await provider.GetValueAsync();","handlingStrategy":"try-catch","validationCode":"// No public IsSet property; wrap reads\nasync Task<string?> SafeGetAsync(Provider p, CancellationToken ct)\n{\n    try { return await p.GetValueAsync(ct); }\n    catch (InvalidOperationException) { return null; }\n}","typeGuard":null,"tryCatchPattern":"string? value;\ntry\n{\n    value = await provider.GetValueAsync(ct);\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"Value for provider has not been set.\")\n{\n    logger.LogError(ex, \"Provider read before configuration completed.\");\n    throw;\n}","preventionTips":["Only read provider values after the provisioning/configuration phase completes","Ensure every code path that creates a provider also calls Set","Sequence GetValueAsync calls after Set in tests and startup code"],"tags":["azure","foundry","state","lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"25830f84bd145686607ad00c057b3f84e2e51d43","analyzedAt":"2026-09-16T11:10:06.193Z","contentChangedAt":"2026-09-16T11:10:06.193Z","schemaVersion":2},"datasetVersion":"2026-09-21T04:17:39.646Z"}