Support license key via environment variable fallback#1175
Merged
Conversation
If no license key is set in code, MediatR now resolves one from environment variables: the product-specific MEDIATR_LICENSE_KEY, then the shared LUCKYPENNY_LICENSE_KEY (usable across Lucky Penny products). Resolution uses the first non-blank value in order of precedence: explicit config key, static Mediator.LicenseKey, MEDIATR_LICENSE_KEY, then LUCKYPENNY_LICENSE_KEY. Blank values are treated as unconfigured and fall through, consistent with #1170. Ports LuckyPennySoftware/AutoMapper#4634 to MediatR. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Contributor
There was a problem hiding this comment.
Pull request overview
Adds environment-variable fallback for MediatR license key resolution so container/cloud deployments can supply the key without code changes, while preserving existing “blank means unconfigured” semantics.
Changes:
- Introduces
LicenseAccessor.ResolveLicenseKey(...)to resolve the first non-blank key from explicit config/static sources, then environment variables. - Adds tests covering precedence, blank handling, and the all-unset case (with parallelization intended to be disabled due to process-global env var mutation).
- Updates README to document
MEDIATR_LICENSE_KEYandLUCKYPENNY_LICENSE_KEYdiscovery and precedence.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| test/MediatR.Tests/Licensing/LicenseKeyEnvironmentVariableTests.cs | Adds unit tests for environment-variable license key fallback and precedence rules. |
| src/MediatR/Licensing/LicenseAccessor.cs | Implements env-var fallback via a resolver and applies blank-as-unconfigured behavior in initialization. |
| README.md | Documents environment-variable auto-discovery and precedence order. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
- Move DisableParallelization to a dedicated collection-definition class and apply only [Collection] to the test class, matching the existing ServiceFactoryCollectionBehavior pattern. - Rewrite ResolveLicenseKey imperatively so environment variables are only read when no explicit key is configured, avoiding eager reads. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This was referenced Jul 12, 2026
Merged
Merged
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.
What Changed
Added support for reading the MediatR license key from an environment variable instead of requiring it to be set in code. Ports LuckyPennySoftware/AutoMapper#4634 to MediatR.
Why
In containerized and cloud environments it's preferable to supply sensitive configuration through environment variables rather than hardcoding it. It also lets enterprises share a single key across many services without code changes.
How It Works
If no license key is set in code, MediatR resolves one from environment variables. The key is resolved using the first non-blank value in this order of precedence:
cfg.LicenseKeyorMediator.LicenseKey).MEDIATR_LICENSE_KEYenvironment variable (product-specific).LUCKYPENNY_LICENSE_KEYenvironment variable (shared across Lucky Penny products; for example, AutoMapper reads the same variable).Blank/whitespace values are treated as unconfigured and fall through, consistent with #1170.
Notes on adapting from the AutoMapper PR
Mediator.LicenseKey) where AutoMapper has one, soResolveLicenseKeyacceptsparams string?[]to cover both.ci.ymlchanges were not ported — they're AutoMapper-specific, and MediatR already has fork-safe CI (commits b28cfac, 329ce79).Tests
New
LicenseKeyEnvironmentVariableTestscovering explicit-key precedence, config-vs-static precedence, per-variable fallback, blank handling, and the all-unset case. Parallelization is disabled for the class since it mutates process-global environment variables.Backward Compatibility
100% backward compatible — existing code that sets the license key continues to work unchanged.
🤖 Generated with Claude Code