Fix AddCarter when DependencyContext is unavailable in single-file publish#386
Open
seifmaazouz wants to merge 1 commit into
Open
Fix AddCarter when DependencyContext is unavailable in single-file publish#386seifmaazouz wants to merge 1 commit into
seifmaazouz wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
DependencyContext.Load()returnsnullfor single-file published apps (see dotnet/runtime#70438). Carter currently assumes aDependencyContextis always available, causingAddCarter()to fail during startup. This PR adds a fallback for that scenario while leaving the existingDependencyContextpath unchanged.Changes
AppDomain.CurrentDomain.GetAssemblies()whenDependencyContextis unavailable.DependencyContextis available.Testing
Normal application
Verified that discovery matches the current behavior:
Single-file publish
Published the sample application as a single-file executable and verified that:
AddCarter()no longer throws during startup.Observed counts:
The missing validator belongs to an assembly that isn't loaded by the runtime in the single-file application, so it can't be discovered once
DependencyContextis unavailable.During testing I found that scanning all loaded assemblies includes FluentValidation's own open generic validator types (for example
InlineValidator<T>). Without the generic parameter check, DI registration fails with:This PR skips validators that still have unbound generic parameters to avoid that failure.
Notes
The fallback can only discover assemblies that have already been loaded by the runtime. Assemblies that are never loaded can't be discovered once
DependencyContextis unavailable. This matches the limitation described in dotnet/runtime#70438.I intentionally left the fallback unfiltered (it scans all loaded assemblies) because reducing that scan would be an optimization rather than a correctness fix. I'd be happy to explore that separately if it would be useful.
Fixes #383.