Skip to content

Microsoft.Extensions.AmbientMetadata.Build does not support netstandard2.0 and net4x #7415

@CZEMacLeod

Description

@CZEMacLeod

Description

The Microsoft.Extensions.AmbientMetadata.Application package targets and supports the standard TFMs as well as netstandard2.0 and net462.
The Microsoft.Extensions.AmbientMetadata.Build package does not include these extra TFMs and cannot be used on those platforms.
I don't see any specific code that would prevent it working on those additional platforms.

Manually adding BuildMetadata.cs and BuildMetadataServiceCollectionExtensions.cs to a netstandard2.0 project worked (except the embedded version of Throw.IfNull) so I think it should just work by changing the TFMs for this package.

Reproduction Steps

Create a netstandard2.0 project and add references to Microsoft.Extensions.AmbientMetadata.Application and Microsoft.Extensions.AmbientMetadata.Build.

Try and reference IOptions<ApplicationMetadata>- Success.
Try and reference IOptions<BuildMetadata> - Fails.

Expected behavior

The following code snippet should compile in a netstandard2.0 class library or a net4x application.

    public static void LogAmbient(IServiceProvider serviceProvider)
    {
        var logger = serviceProvider.GetRequiredService<ILoggerFactory>().CreateLogger("Ambient");
        var application = serviceProvider.GetRequiredService<IOptions<ApplicationMetadata>>().Value;
        var build = serviceProvider.GetRequiredService<IOptions<BuildMetadata>>().Value;

        logger.LogInformation("Application {Name} {Version}", application.ApplicationName, application.BuildVersion);
        logger.LogInformation("Build Information {Number} {Id}", build.BuildNumber, build.BuildId);
        logger.LogInformation("Source {Branch}/{Version}", build.SourceBranchName, build.SourceVersion);
    }

Actual behavior

Code does not compile unless targeting net8.0, net9.0, or net10.0.

BuildMetadata type does not exist on other platforms (ApplicationMetadata does exist)

Regression?

No response

Known Workarounds

It is almost possible to get this to work by embedding the types mentioned and forcing the Microsoft.Extensions.AmbientMetadata.Build package to be private to avoid pulling it into a net10.0 project.
Alternatively, multitargeting with #if !NET clauses around the code can also work but either solution is not ideal.

Configuration

Not really applicable, but netstandard2.0.
Using NET10 SDK and referencing the classlibrary from both a net10.0 project and a net481 project.

Other information

vs

<TargetFrameworks>$(NetCoreTargetFrameworks);netstandard2.0;net462</TargetFrameworks>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugThis issue describes a behavior which is not expected - a bug.needs-area-labeluntriaged

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions