Skip to content

Fix runtime version detection for netstandard2.0 on .NET Framework#196

Merged
Mpdreamz merged 1 commit intomainfrom
fix/netstandard2-runtime-version-detection
Feb 23, 2026
Merged

Fix runtime version detection for netstandard2.0 on .NET Framework#196
Mpdreamz merged 1 commit intomainfrom
fix/netstandard2-runtime-version-detection

Conversation

@Mpdreamz
Copy link
Member

@Mpdreamz Mpdreamz commented Feb 23, 2026

Summary

Fixes #192

  • Adds a fallback in GetNetCoreVersion() that detects .NET Framework from RuntimeInformation.FrameworkDescription when the netstandard2.0 build is loaded by a .NET Framework host
  • Extracts a semver-compatible major.minor.0 version string (e.g. "4.8.0" from ".NET Framework 4.8.4614.0")
  • Only activates when all other detection methods have already failed, so existing .NET Core/.NET 5+ code paths are unaffected

Background

When the netstandard2.0 build runs on .NET Framework (e.g. 4.7.2), the #if !NETFRAMEWORK code path is taken and GetNetCoreVersion() is called instead of GetFullFrameworkRuntime(). All detection strategies fail:

  1. Environment.Version.Major is 4 (not >= 5)
  2. TargetFrameworkAttribute.FrameworkName is .NETFramework,Version=v4.7.2 — doesn't match the .NETCoreApp,Version=v prefix
  3. DOTNET_RUNNING_IN_CONTAINER env var is not set

This caused two issues:

  • Pre-8f2386d: the returned null was passed to SemVersion.TryParseRegex.Match(null)ArgumentNullException on client initialization
  • Post-8f2386d: the null guard returns Empty (0.0.0), producing incorrect net=0.0.0 in the metadata telemetry header

Made with Cursor

)

When the netstandard2.0 build of Elastic.Transport is loaded by a .NET
Framework host (e.g. 4.7.2), the `#if !NETFRAMEWORK` code path is taken
and `GetNetCoreVersion()` is called instead of `GetFullFrameworkRuntime()`.

All detection strategies in `GetNetCoreVersion` fail on .NET Framework:
- `Environment.Version.Major` is 4 (not >= 5)
- `TargetFrameworkAttribute.FrameworkName` is `.NETFramework,Version=v4.7.2`
  which doesn't match the `.NETCoreApp,Version=v` prefix
- `DOTNET_RUNNING_IN_CONTAINER` env var is not set

This caused two problems:
1. On older versions (pre-8f2386d), the returned null was passed to
   `SemVersion.TryParse` which called `Regex.Match(null)`, throwing an
   `ArgumentNullException` on client initialization.
2. After the null guard added in 8f2386d, the version silently resolves
   to "0.0.0" instead of the actual framework version, producing
   incorrect `net=` values in the metadata telemetry header.

This commit adds a fallback in `GetNetCoreVersion` that detects .NET
Framework from `RuntimeInformation.FrameworkDescription` (which reports
e.g. ".NET Framework 4.8.4614.0") and extracts a semver-compatible
major.minor.0 version string. This only activates when all other
detection methods have already failed, so existing code paths for
.NET Core/.NET 5+ are unaffected.

Closes #192

Co-authored-by: Cursor <cursoragent@cursor.com>
Copy link
Member

@flobernd flobernd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! That was on my TODO for way too long already.

@Mpdreamz Mpdreamz merged commit a0b3e44 into main Feb 23, 2026
7 checks passed
@Mpdreamz Mpdreamz deleted the fix/netstandard2-runtime-version-detection branch February 23, 2026 18:20
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.

.NET version detection doesn't work as expected for netstandard2.0 builds

2 participants