microsoft/aspire · error · ArgumentException

Launch profile must be a string or ProjectResourceOptions.

Error message

Launch profile must be a string or ProjectResourceOptions.

What it means

AddProjectForPolyglot throws ArgumentException ( paramName launchProfileOrOptions) when the launch profile argument is neither null, a string, nor a ProjectResourceOptions instance. The method uses a switch expression over the argument; any other object type falls into the discard arm and fails fast. It exists to accept a launch profile name or options object; anything else is a caller type error.

Solutions

  1. Pass a string launch profile name, a ProjectResourceOptions instance, or null.
  2. If you have a profile object, pass its Name string instead.
  3. Use ProjectResourceOptions with the configurator callback for advanced settings.

Example fix

// before
builder.AddProjectForPolyglot("api", projectPath, someProfileObject);

// after
builder.AddProjectForPolyglot("api", projectPath, someProfileObject.Name);
// or
builder.AddProjectForPolyglot("api", projectPath, new ProjectResourceOptions { ... });
Defensive patterns

Strategy: validation

Validate before calling

if (launchProfileOrOptions is not null && launchProfileOrOptions is not string && launchProfileOrOptions is not ProjectResourceOptions)
{
    throw new ArgumentException("Launch profile must be a string or ProjectResourceOptions.", nameof(launchProfileOrOptions));
}

Type guard

bool isValidLaunchProfileArg(object? arg) => arg is null or string or ProjectResourceOptions;

Try / catch

try
{
    builder.AddProjectForPolyglot(name, path, launchProfileOrOptions);
}
catch (ArgumentException ex) when (ex.ParamName == nameof(launchProfileOrOptions))
{
    logger.LogError(ex, "Invalid launch profile argument type: {Type}", launchProfileOrOptions?.GetType().Name);
}

Prevention

When it happens

Trigger: Passing an unsupported type as launchProfileOrOptions, e.g. an int, a LaunchSettingsProfile object, or some other custom type instead of string or ProjectResourceOptions.

Common situations: Refactoring code where a profile object was passed instead of its name, or overload-resolution confusion where a caller assumed a richer type was accepted.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Aspire.Hosting/ProjectResourceBuilderExtensions.cs:130

    /// Adds a .NET project resource
    /// </summary>
    [AspireExport("addProject")]
    internal static IResourceBuilder<ProjectResource> AddProjectForPolyglot(
        this IDistributedApplicationBuilder builder,
        [ResourceName] string name,
        string projectPath,
        [AspireUnion(typeof(string), typeof(ProjectResourceOptions))] object? launchProfileOrOptions = null)
    {
        ArgumentNullException.ThrowIfNull(builder);
        ArgumentException.ThrowIfNullOrEmpty(name);
        ArgumentNullException.ThrowIfNull(projectPath);

        return launchProfileOrOptions switch
        {
            null => builder.AddProject(name, projectPath),
            string launchProfileName => builder.AddProject(name, projectPath, launchProfileName),
            ProjectResourceOptions options => builder.AddProject(name, projectPath, configure => ApplyProjectResourceOptions(configure, options)),
            _ => throw new ArgumentException("Launch profile must be a string or ProjectResourceOptions.", nameof(launchProfileOrOptions))
        };
    }

    /// <summary>
    /// Adds a .NET project to the application model. By default, this will exist in a Projects namespace. e.g. Projects.MyProject.
    /// If the project is not in a Projects namespace, make sure a project reference is added from the AppHost project to the target project.
    /// </summary>
    /// <typeparam name="TProject">A type that represents the project reference.</typeparam>
    /// <param name="builder">The <see cref="IDistributedApplicationBuilder"/>.</param>
    /// <param name="name">The name of the resource. This name will be used for service discovery when referenced in a dependency.</param>
    /// <param name="launchProfileName">The launch profile to use. If <c>null</c> then no launch profile will be used.</param>
    /// <returns>A reference to the <see cref="IResourceBuilder{T}"/>.</returns>
    /// <remarks>
    /// <para>
    /// This overload of the <see cref="AddProject{TProject}(IDistributedApplicationBuilder, string)"/> method takes
    /// a <typeparamref name="TProject"/> type parameter. The <typeparamref name="TProject"/> type parameter is constrained
    /// to types that implement the <see cref="IProjectMetadata"/> interface.
    /// </para>

View on GitHub (pinned to 25830f84bd)