microsoft/aspire · error · ArgumentException

Both SourcePath and Contents are set for a file entry

Error message

Both SourcePath and Contents are set for a file entry

What it means

Thrown when a ContainerFile entry specifies both a SourcePath (a file to copy from the host) and inline Contents. These are mutually exclusive ways to supply the file body, so the converter rejects the entry with an ArgumentException during DCP model serialization.

Solutions

  1. Remove one of the two: keep SourcePath to copy from disk, or keep Contents for inline content.
  2. If both behaviors are needed, create two separate file entries (one sourced, one inline) at different destinations.
  3. Centralize file-entry construction in a helper that asserts only one is set before building the ContainerFile.

Example fix

// before
new ContainerFile { SourcePath = "./conf/app.conf", Contents = "key=value", Destination = "/etc/app.conf" }
// after
new ContainerFile { SourcePath = "./conf/app.conf", Destination = "/etc/app.conf" }
Defensive patterns

Strategy: validation

Validate before calling

if (file.SourcePath is not null && file.Contents is not null)
    throw new ArgumentException("Set either SourcePath or Contents, not both.");

Try / catch

try { AddFileEntry(file); }
catch (ArgumentException ex) when (ex.Message.Contains("SourcePath")) { /* fix config: pick one source */ }

Prevention

When it happens

Trigger: Constructing a ContainerFile (or using the corresponding resource builder API) with both Contents set to a non-null string and SourcePath set to a non-null path, then adding it to a container's file annotations.

Common situations: Copy-pasted configuration where a template sets Contents but a caller also fills in SourcePath; merging two configuration sources that each supplied one of the fields.

Understand the failure class

Background: Conflicting config options: "cannot be used together" — configuration validation errors across open-source libraries — this error's family across 162 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/Dcp/Model/Container.cs:405

        var entry = new ContainerFileSystemEntry
        {
            Type = type,
            Name = item.Name,
            Owner = item.Owner,
            Group = item.Group,
            Mode = (int)item.Mode,
        };

        if (item is ContainerFileBase file)
        {
            entry.Source = file.SourcePath;
            entry.Contents = file.Contents;
            entry.ContinueOnError = file.ContinueOnError;

            if (file.Contents is not null && file.SourcePath is not null)
            {
                throw new ArgumentException("Both SourcePath and Contents are set for a file entry");
            }
        }
        else if (item is ContainerDirectory directory)
        {
            entry.Entries = directory.Entries?.Select(e => e.ToContainerFileSystemEntry()).ToList();
        }

        return entry;
    }
}

internal sealed class ContainerFileSystemEntry : IEquatable<ContainerFileSystemEntry>
{
    // The type of the file system entry (file or directory)
    [JsonPropertyName("type")]
    public string Type { get; set; } = ContainerFileSystemEntryType.File;

    // The name of the file system entry

View on GitHub (pinned to 25830f84bd)