{"record":{"id":"b38d90a36f197332","repo":"microsoft/aspire","slug":"configuring-programmatically-clusters-while-providing-a","errorCode":null,"errorMessage":"Configuring programmatically clusters while providing a configuration file isn't supported","messagePattern":"Configuring programmatically clusters while providing a configuration file isn't supported","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Aspire.Hosting.Yarp/YarpJsonConfigGeneratorBuilder.cs","lineNumber":34,"sourceCode":"    private readonly List<RouteConfig> _routeConfigs = new List<RouteConfig>();\n    private readonly JsonSerializerOptions _serializerOptions;\n\n    public YarpJsonConfigGeneratorBuilder()\n    {\n        _serializerOptions = new JsonSerializerOptions()\n        {\n            WriteIndented = true,\n            DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,\n        };\n        _serializerOptions.Converters.Add(new SslProtocolsConverter());\n        _serializerOptions.Converters.Add(new JsonStringEnumConverter(new PascalCaseJsonNamingPolicy()));\n    }\n\n    public IYarpJsonConfigGeneratorBuilder AddCluster(ClusterConfig cluster)\n    {\n        if (_configFilePath != null)\n        {\n            throw new ArgumentException(\"Configuring programmatically clusters while providing a configuration file isn't supported\");\n        }\n        _clusterConfigs.Add(cluster);\n        return this;\n    }\n\n    public IYarpJsonConfigGeneratorBuilder AddRoute(RouteConfig route)\n    {\n        if (_configFilePath != null)\n        {\n            throw new ArgumentException(\"Configuring programmatically routes while providing a configuration file isn't supported\");\n        }\n        _routeConfigs.Add(route);\n        return this;\n    }\n\n    public IYarpJsonConfigGeneratorBuilder WithConfigFile(string configFilePath)\n    {\n        if (_clusterConfigs.Count > 0 || _routeConfigs.Count > 0)","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/microsoft/aspire/blob/25830f84bd145686607ad00c057b3f84e2e51d43/src/Aspire.Hosting.Yarp/YarpJsonConfigGeneratorBuilder.cs#L16-L52","documentation":"Thrown by YarpJsonConfigGeneratorBuilder.AddCluster when a YARP config file was already supplied via WithConfigFile. The generator supports exactly one configuration source — either the JSON file or programmatic route/cluster definitions — never both, so it fails fast on mixing them.","triggerScenarios":"Calling withYarpConfigFile(...).AddCluster(...) — i.e. AddCluster after WithConfigFile on the same IYarpJsonConfigGeneratorBuilder.","commonSituations":"Starting from a config-file sample then adding one cluster in code; a shared setup helper that always calls AddCluster while a file was configured; migration halfway between file-based and code-based config.","solutions":["Pick one source: remove the WithConfigFile call if you want programmatic clusters.","Or move the cluster definition into the JSON config file and drop the AddCluster call.","Refactor so WithConfigFile and AddCluster/AddRoute are mutually exclusive branches based on configuration."],"exampleFix":"// before\nvar yarp = builder.AddYarp(\"gateway\").WithConfigFile(\"yarp.json\");\nyarp.AddCluster(clusterConfig);\n// after (code-based)\nvar yarp = builder.AddYarp(\"gateway\");\nyarp.AddCluster(clusterConfig);\n// or (file-based): put the cluster in yarp.json and remove AddCluster","handlingStrategy":"validation","validationCode":"if (useConfigFile && clustersToDefine.Count > 0)\n{\n    throw new InvalidOperationException(\"Cannot use a YARP config file together with programmatic AddCluster calls.\");\n}","typeGuard":null,"tryCatchPattern":"catch (ArgumentException ex) when (ex.Message.Contains(\"while providing a configuration file\")) { /* choose one config source and rebuild */ }","preventionTips":["Decide once — file-based or code-based YARP config — and enforce it in app setup.","Don't share helper extensions that blindly call AddCluster on any YARP builder.","Keep clusters in the JSON file when a config file path is already set."],"tags":["yarp","configuration","conflicting-options","json-config"],"backgroundTag":"conflicting-config-options","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"}