Skip to content

Support LicenseKey via environment variable fallback#4634

Open
Nit8 wants to merge 2 commits into
LuckyPennySoftware:mainfrom
Nit8:main
Open

Support LicenseKey via environment variable fallback#4634
Nit8 wants to merge 2 commits into
LuckyPennySoftware:mainfrom
Nit8:main

Conversation

@Nit8
Copy link
Copy Markdown

@Nit8 Nit8 commented Apr 25, 2026

PR: Auto-Detect License Key from Environment Variables Fixes #4631

What Changed

Added support for reading the AutoMapper license key from an environment variable instead of requiring manual configuration in code.

Why

In containerized and cloud environments, it's better to use environment variables for sensitive configuration instead of hardcoding values in code.

How It Works

The LicenseKey property now checks for values in this order:

  1. Explicit value set in code (cfg.LicenseKey = "key") - highest priority
  2. Environment variable AUTOMAPPER_LICENSE_KEY - fallback
  3. Null if neither is set - lowest priority

Usage

NEW WAY - Using Environment Variable:
Set environment variable AUTOMAPPER_LICENSE_KEY before running your app
Then just add AutoMapper without setting the license key:

services.AddAutoMapper(cfg => 
{
    cfg.CreateMap<Foo, FooDto>();
});

OLD WAY - Still Works (Backward Compatible):

services.AddAutoMapper(cfg => 
{
    cfg.LicenseKey = "your-license-key";
    cfg.CreateMap<Foo, FooDto>();
});

BOTH SET - Code value wins:
If you set both the environment variable AND code value, the code value is used.

Tests Added

  • Tests for environment variable auto-detection
  • Tests for old explicit assignment (backward compatibility)
  • Tests for precedence when both are set
  • Integration tests with dependency injection

Backward Compatibility

100% backward compatible. All existing code continues to work exactly the same way.

LicenseKey now falls back to the AUTOMAPPER_LICENSE_KEY environment variable if not explicitly set. Added comprehensive unit and integration tests to verify this behavior. Updated test project dependencies to support DI and logging in tests.
@Nit8
Copy link
Copy Markdown
Author

Nit8 commented Apr 29, 2026

@jbogard can you run this pipeline to check if anything goes wrong

@Nit8
Copy link
Copy Markdown
Author

Nit8 commented May 1, 2026

Hi @jbogard, is this issue related to pipeline, am I missing something?

@jbogard
Copy link
Copy Markdown
Contributor

jbogard commented May 1, 2026

Hmm that is odd, I tried to re-run it but still getting that error.

@Nit8
Copy link
Copy Markdown
Author

Nit8 commented May 4, 2026

yeah @jbogard thats strange does it have something to do with ci pipeline and about read and write permissions?

@Nit8
Copy link
Copy Markdown
Author

Nit8 commented May 19, 2026

Hi @jbogard I see you changed the permission into your release.yml and maybe same has to be done from ci.yml from read to write for this to complete as mentioned in my previous comment, and you can try firing the pipeline to verify. Thanks

@SubashPaudyalTR
Copy link
Copy Markdown

This would be a great relief if we have this directly fed from the configuration, and introduce less noise ... as we are having more than dozens of services which is using automapper and we just acquired the enterprise license. We are looking for best option weather to go with per service configuration v/s one-place shared library. Both holds its own pros and cons, but if we get this env. var auto-discovery, it would be the real winner for us... with minimal changes.

Waiting for this and any other valuable suggestions!!!

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.

Support automatic discovery of license keys

3 participants