Repository navigation
Add LicenseKeyFactory to supply the license key from DI - #1192
Conversation
MediatRServiceConfiguration can't be deferred into IOptions because scanning and registration consume it before a service provider exists. The license key is only read later, on the background validation thread, so it can come from a container service through a factory. The factory runs inside LicenseAccessor.Initialize, keeping slow work such as secret store reads off the Mediator construction path (AutoMapper #4640). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
🟡 Changes recommended
License validation scheduling is non-atomic, allowing duplicate factory calls and retries after failure, while error logging can throw after the initiating scope is disposed and fault the background task.
2 open issues
What changed in this PR
Adds LicenseKeyFactory so MediatR can obtain license keys from dependency-injection services after registration, addressing the use case in #1191 without deferring the full configuration.
Changes:
- Adds a factory receiving the root service provider and integrates it into background license initialization.
- Gives factory results precedence over existing key sources, with blank-value fallback.
- Documents usage and adds tests for resolution, precedence, normalization, threading, and exceptions.
| File | Description |
|---|---|
| test/MediatR.Tests/Licensing/LicenseKeyFactoryTests.cs | Adds factory behavior and background-execution tests. |
| src/MediatR/Registration/ServiceRegistrar.cs | Passes the service provider to the license accessor. |
| src/MediatR/MicrosoftExtensionsDI/MediatrServiceConfiguration.cs | Exposes and documents LicenseKeyFactory. |
| src/MediatR/Licensing/LicenseAccessor.cs | Evaluates the factory before other license key sources. |
| README.md | Documents factory usage, constraints, and precedence. |
Review effort: Balanced
ILicenseKeyProvider read like a type that ships with MediatR. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…posed Concurrent first Mediator resolutions could each start validation, and a thread waiting on the LicenseAccessor lock could create the license again because the result was assigned after the lock was released. Either way, the factory could run more than once. The catch in ValidateLicense also resolved ILoggerFactory from the provider that created the Mediator. With a disposed request scope, that threw inside the catch, losing the error and faulting the background task. Get the logger before starting validation instead. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Summary
Closes #1191.
License keys delivered by a service in the container, such as a secret store client, can't be set during
AddMediatR, because no service provider exists yet. This adds a factory just for the key:Why not
AddOptions<MediatRServiceConfiguration>(), as the issue proposesAddMediatRconsumes assemblies, behaviors and lifetimes while registering services, soMediatRServiceConfigurationcan't be deferred the way AutoMapper defersMapperConfigurationExpression. AutoMapper can do that because its scan assemblies are method arguments, not configuration.Today the issue's snippet compiles but the key is ignored, which I confirmed with a throwaway app. Honoring it would mean supporting
LicenseKeywhile every other setting on that class silently does nothing when set there. It would also add aMicrosoft.Extensions.Optionsdependency.The license key is the one setting that can be late: it's only read after the first
Mediatoris created.Behavior
LicenseAccessor.Initialize, on the existing background validation thread. Slow work such as a secret store read can't blockMediatorconstruction, which is the path behind the AutoMapper #4640 deadlock.LicenseKeyFactory,cfg.LicenseKey,Mediator.LicenseKey,MEDIATR_LICENSE_KEY, thenLUCKYPENNY_LICENSE_KEY. A null or blank result falls through to the next source.Mediatorhas been disposed, and doesn't affectMediator. Validation is scheduled atomically, so the factory isn't retried.CheckLicensewasn't atomic,LicenseAccessorassigned its result outside the lock, and the error log could fail with a disposed scope. A slow factory makes all three much easier to hit.AddMediatRcalls: the first configuration wins, the same asLicenseKeytoday, because the configuration is registered withTryAddSingleton.IConfigureOptions<>isn't added here. A separate license options type could add it later if there's demand.Tests
LicenseKeyFactoryTestscovers resolving from a container service, precedence overLicenseKey, blank fallback, whitespace stripping, not running during registration, not blocking construction, and logging exceptions, including after the initiating scope is disposed.Mediatorresolutions and for concurrent reads ofLicenseAccessor.Current._licenseinside the lock, and getting the logger before scheduling.🤖 Generated with Claude Code