skip to content

In MediatR 14, where does the Lucky Penny licence key come from, and what happens at runtime when it is missing, invalid or expired?

level: seniorimportance: should knowfreq 40%

answer

  1. cfg.LicenseKey or Mediator.LicenseKey
  2. MEDIATR_LICENSE_KEY, then LUCKYPENNY_LICENSE_KEY
  3. signed token, validated offline
  4. log lines only, nothing disabled
  5. LuckyPennySoftware.MediatR.License category

basics

~10 s

MediatR 14 reads the key from cfg.LicenseKey or Mediator.LicenseKey, then MEDIATR_LICENSE_KEY, then LUCKYPENNY_LICENSE_KEY. A missing, invalid or expired key never disables anything: MediatR only writes log entries under the LuckyPennySoftware.MediatR.License category.

solid answer

~50 s

The key is resolved once, in order: an explicit value (`cfg.LicenseKey` in `AddMediatR`, or the static `Mediator.LicenseKey` without Microsoft DI), then the `MEDIATR_LICENSE_KEY` environment variable, then the shared `LUCKYPENNY_LICENSE_KEY`. It is a signed token MediatR validates locally with an embedded public key, with no network call. Validation runs once, on a dedicated background thread started when the first `Mediator` is constructed, and it **only logs**: no key logs a warning saying that is allowed for development and testing but production requires a licence; an expired key (unless a perpetual licence covers that build) or one that does not cover MediatR logs errors; a valid one logs its edition and expiry. `Send` and `Publish` behave identically either way. The README shows filtering the category with `AddFilter(..., LogLevel.None)`, but that hides the message, not the licence terms.

code

bash · 1 line
bash
docker run -e MEDIATR_LICENSE_KEY="$MEDIATR_LICENSE_KEY" takeaway-orders-api:latest

go deeper

for a junior

Recall that MediatR 14 expects a licence key, set with cfg.LicenseKey or an environment variable, and that a missing key produces a log warning rather than an error.

for a middle

Explain the resolution order, code then MEDIATR_LICENSE_KEY then LUCKYPENNY_LICENSE_KEY, and that validation is an offline signature check that only writes log entries.

for a senior

Show the operational view: keep the key in a secret store, alert on the licence log category instead of silencing it, and know that nothing fails closed when a key expires.

for a principal

Treat licence enforcement by logging as a compliance process question: who tracks expiry and product coverage across services, and how audits find unlicensed production use.

## Where the key comes from MediatR 14 is a Lucky Penny Software product. Its `LICENSE.md` offers the code under the **Reciprocal Public License 1.5**, or, for anyone who does not want to release their own source under those terms, under Lucky Penny's paid licence agreement. The code expresses that through a licence key, resolved in this order, first non-blank value wins: 1. **`cfg.LicenseKey`** on `MediatRServiceConfiguration`, set inside `AddMediatR`, or the static **`Mediator.LicenseKey`** when you are not using Microsoft DI; 2. the **`MEDIATR_LICENSE_KEY`** environment variable; 3. the **`LUCKYPENNY_LICENSE_KEY`** environment variable, shared across Lucky Penny products (AutoMapper reads it too). A shared key must be for a licence that includes MediatR, meaning a MediatR or Bundle product type; an AutoMapper-only key is rejected. The environment-variable fallback is recent: the 14.2.0 tag sits on the merge of a branch named `license-key-env-var`, so on an older 14.x build set the key in code. ## How it is checked - The key is a **signed token**. MediatR validates the signature against a public key compiled into the assembly, with an issuer of `https://luckypennysoftware.com`. Nothing is sent over the network. - The check runs **once**, triggered by the first `Mediator` constructor after `AddMediatR`, on a **dedicated long-running thread**. The source explains why: running it inline under the DI container's singleton-build lock could deadlock a starving thread pool. - Every exception inside the check is caught and logged; none reaches your code. ## What each outcome does | Situation | Log level | Effect on Send/Publish | |---|---|---| | No key anywhere | Warning: allowed for development and testing, production requires a licensed version | none | | Signature does not validate | Critical, then treated as no key (Warning) | none | | Expired | Error ("expired N days ago") | none | | Expired, perpetual, MediatR build dated before expiry | Information | none | | Product does not include MediatR | Error | none | | Valid | Information: product, edition, expiry date | none | All entries use the category **`LuckyPennySoftware.MediatR.License`**. The README notes that client applications such as Blazor WebAssembly do not need the key, and shows silencing the category with `builder.Logging.AddFilter("LuckyPennySoftware.MediatR.License", LogLevel.None)`. ## What that means in practice - **Nothing fails closed.** A rotated, expired or mistyped key in production will not take the service down; it will only produce log entries. That is good for availability, and it also means nobody notices unless someone watches for them. - **Hiding the log is not compliance.** The obligation sits in the licence terms; the code merely reports. Filtering the category to `None` in production removes the only signal that the key is wrong. - **The perpetual flag.** A licence whose token carries the perpetual claim keeps validating after expiry for MediatR builds dated on or before the expiry date; upgrading to a MediatR build released after the expiry brings the expired error back. ## Supplying the key Keep the key out of source control like any credential: read it from configuration backed by a secret store, or set the environment variable in the container or hosting platform. ```csharp builder.Services.AddMediatR(cfg => { cfg.RegisterServicesFromAssemblyContaining<PlaceOrderHandler>(); cfg.LicenseKey = builder.Configuration["MediatR:LicenseKey"]; }); ``` If that configuration value is empty, MediatR falls through to the environment variables, so one deployment can use either mechanism. ## Watching it in production Because nothing fails, the licence state is only visible in logs. A sensible setup: - keep the category at `Warning` or above in production rather than `None`; - add a log-based alert on any entry from `LuckyPennySoftware.MediatR.License` at `Error` or `Critical`, which catches an expired, wrong-product or corrupted key; - record the expiry date the `Information` entry reports at startup, and renew before it. In tests and local development the missing-key warning is expected; the warning text itself says that is allowed for development and testing scenarios. Remember the registration rule from the same call: setting only the key, with no assembly registered, makes `AddMediatR` throw `ArgumentException`.

  • Your company also licenses AutoMapper. Can one environment variable serve both libraries?
    `LUCKYPENNY_LICENSE_KEY` is read by both, but MediatR's check logs an error unless the key's product type is MediatR or Bundle, so an AutoMapper-only key does not cover it. A `MEDIATR_LICENSE_KEY` value, if present, takes precedence over the shared variable, and a key set in code beats both.
  • Why does a bad MediatR key neither crash the app nor slow its startup?
    Validation is logging-only and runs on a dedicated background thread (`TaskCreationOptions.LongRunning`) started the first time a `Mediator` is constructed, and only once. Exceptions are caught and logged. The source notes that running it inline under the container's singleton-build lock could deadlock under thread-pool starvation.
  • What does the perpetual claim in a MediatR licence change?
    An expired key normally logs an error. If the key carries the perpetual claim and the running MediatR assembly's embedded build date is on or before the licence expiry, MediatR logs information instead, so builds released during the subscription keep validating; a newer build gets the expired error.

  • Without a licence key, MediatR 14 throws at startup.
  • An expired licence key disables Send and Publish.
  • MediatR phones home to validate the key on every startup.
  • MEDIATR_LICENSE_KEY overrides a key set in cfg.LicenseKey.
  • Filtering the licence log category removes the need for a licence.

Assumes MediatR 14.2, which resolves the key from cfg.LicenseKey or Mediator.LicenseKey, then MEDIATR_LICENSE_KEY, then LUCKYPENNY_LICENSE_KEY, and whose validation only logs.