Skip to content

Support single-file published apps - #390

Merged
jchannon merged 3 commits into
mainfrom
single-file-publish
Nov 12, 2026
Merged

jchannon merged 3 commits into
mainfrom
single-file-publish

Conversation

@jchannon

Copy link
Copy Markdown
Member

Supersedes #386 and fixes #383. The first commit is @seifmaazouz's commit from #386, cherry-picked onto main; the second builds on it.

Problem

Carter discovers modules, validators and response negotiators from the app's DependencyContext. In a single-file published app that context is not usable:

Single-file publish DependencyContext.Load() Before
Self-contained null NullReferenceException in AddCarter
Framework-dependent only the shared framework library starts, every route returns 404

This is unchanged on .NET 11 (dotnet/runtime#70438 is still open).

Changes

  • When the DependencyContext yields no library referencing Carter or FluentValidation, DependencyContextAssemblyCatalog walks the assembly references of the entry assembly and scans the assemblies that reference Carter or FluentValidation. Assemblies named System.* or Microsoft.* are not walked. This replaces Fix AddCarter when DependencyContext is unavailable in single-file publish #386's fallback of scanning every loaded assembly, and also covers the framework-dependent case.
  • Discovery for normal (non single-file) apps is unchanged.
  • Validators with unbound generic parameters are skipped (from Fix AddCarter when DependencyContext is unavailable in single-file publish #386). Without this, a non-abstract open generic validator in a scanned assembly breaks DI registration, in normal apps too.
  • New unit tests for the reference walk and the open generic validator filter.
  • New single-file-smoke-test.sh, run as a separate CI job, publishes the sample as a single file (framework-dependent and self-contained) and checks a route and a validator from a referenced project.
  • README section on single-file publishing.

Known limitation

The compiler drops references to assemblies whose types are never used in code, so a project referenced only for its modules or validators is not found by the walk. Those apps need to pass assemblies explicitly with new DependencyContextAssemblyCatalog(assembly1, assembly2), which is documented in the README.

🤖 Generated with Claude Code

seifmaazouz and others added 3 commits November 12, 2026 12:06
DependencyContext.Load() returns null for single-file published apps,
causing AddCarter() to fail during startup.

Fall back to AppDomain.CurrentDomain.GetAssemblies() when
DependencyContext is unavailable.

Also skip open generic validators during discovery, since the
fallback scans FluentValidation's own assembly and those types
cannot be registered in DI.
In single-file published apps the DependencyContext is either null
(self-contained) or only describes the shared framework
(framework-dependent), so AddCarter either threw or found no modules.

When the DependencyContext yields no library referencing Carter or
FluentValidation, walk the assembly references of the entry assembly
instead and scan the assemblies that reference Carter or
FluentValidation. This replaces scanning every loaded assembly.

Add unit tests, a single-file smoke test that runs in CI and README
documentation.

Co-Authored-By: Claude <noreply@anthropic.com>
Add a sample app that is published as a single file, with a module and
validator of its own and a validator in the referenced
ValidatorOnlyProject.

Point the single-file smoke test at the new sample and split the script
into small functions. Run it as a step in the build job, before the
packaging and push steps, so a failure blocks merging and publishing.
The publish no longer regenerates the NuGet packages.

Co-Authored-By: Claude <noreply@anthropic.com>
@jchannon jchannon added this to the vNext milestone Nov 12, 2026
@jchannon
jchannon merged commit 75f1d11 into main Nov 12, 2026
1 check passed
@jchannon
jchannon deleted the single-file-publish branch November 12, 2026 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

AddCarter failes if project is published as single file

2 participants