RicoSuter/NSwag · error · InvalidOperationException

Unsupported target framework

Error message

Unsupported target framework '{projectMetadata.TargetFrameworkIdentifier}'.

What it means

After resolving MSBuild metadata, RunAsync dispatches to a launcher appropriate to the project's TargetFrameworkIdentifier. It supports .NET Framework (NET462 build) and .NET Core (identifiers equal to '.NETCoreApp' or starting with 'net'); anything else — e.g. a netstandard class library or an unrecognized identifier — results in this 'Unsupported target framework' error.

Solutions

  1. Run the command against an executable ASP.NET Core project (Microsoft.NET.Sdk.Web) with a supported <TargetFramework> (netcoreapp/netN or net4x).
  2. Install the NSwag flavor matching your project: NSwag.ConsoleCore for .NET Core/5+ projects, NSwag.Console for .NET Framework projects.
  3. Explicitly pass --targetframework matching the project's TFM if multi-targeting is confusing metadata evaluation.
  4. Fix a missing or invalid <TargetFramework> element in the .csproj.

Example fix

// before (csproj)
<TargetFramework>netstandard2.0</TargetFramework>
// after
<TargetFramework>net8.0</TargetFramework>
Defensive patterns

Strategy: validation

Validate before calling

var tfm = projectMetadata.TargetFrameworkIdentifier;
var supported = tfm == ".NETFramework" || tfm == ".NETCoreApp" || tfm.StartsWith("net");
if (!supported)
    throw new InvalidOperationException($"Project must target net4x or netcoreapp/netN, got: {tfm}");

Try / catch

try
{
    await command.RunAsync(processor, host);
}
catch (InvalidOperationException ex) when (ex.Message.StartsWith("Unsupported target framework"))
{
    // choose the NSwag flavor matching the project TFM or fix the csproj TFM
}

Prevention

When it happens

Trigger: Pointing aspnetcore2openapi at a project whose evaluated TargetFrameworkIdentifier is neither '.NETFramework'/'net'-prefixed Core App (e.g. '.NETStandard', or metadata evaluation returned empty/garbled because the project is not an executable ASP.NET Core app).

Common situations: Accidentally targeting a netstandard class library that hosts controllers but is not a runnable app; malformed or missing TargetFramework in the csproj; custom SDKs that report unusual framework identifiers; running the .NET Framework NSwag build against a .NET 5+ project (or vice versa) with mismatched identifier parsing.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of RicoSuter/NSwag@63daf8fcc3 (2026-09-14). Data as JSON: /api/errors/88ffa4413f479e8b. Report an issue: GitHub.

Appendix: source

Thrown at src/NSwag.Commands/Commands/Generation/AspNetCore/AspNetCoreToOpenApiCommand.cs:173

                var executorBinary = Path.Combine(toolDirectory, binaryName);

                if (!File.Exists(executorBinary))
                {
                    binaryName = LauncherBinaryName + ".exe";
                    executorBinary = Path.Combine(toolDirectory, binaryName);
                }

                if (!File.Exists(executorBinary))
                {
                    throw new InvalidOperationException($"Unable to locate {binaryName} in {toolDirectory}.");
                }

                args.Add(executorBinary);
            }
#endif
            else
            {
                throw new InvalidOperationException($"Unsupported target framework '{projectMetadata.TargetFrameworkIdentifier}'.");
            }

            var commandFile = Path.GetTempFileName();
            var outputFile = Path.GetTempFileName();
            File.WriteAllText(commandFile, JsonConvert.SerializeObject(this));
            cleanupFiles.Add(commandFile);
            cleanupFiles.Add(outputFile);

            args.Add(commandFile);
            args.Add(outputFile);
            args.Add(projectMetadata.AssemblyName);
            args.Add(toolDirectory);

            try
            {
                var exitCode = await Exe.RunAsync(executable, args, verboseHost).ConfigureAwait(false);
                if (exitCode != 0)
                {

View on GitHub (pinned to 63daf8fcc3)