dotnet / dotnet/extensions

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

Open
#7,415 1 comment 0 reactions 0 assignees View on GitHub
bug needs-area-label untriaged
Dominant language
C#
Stars
3.2k
Forks
894
Avg merge
1d 12h
Merged PRs (30d)
23

Description

### 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`- Success.
Try and reference `IOptions` - Fails.

### Expected behavior

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

```cs
public static void LogAmbient(IServiceProvider serviceProvider)
{
var logger = serviceProvider.GetRequiredService().CreateLogger("Ambient");
var application = serviceProvider.GetRequiredService>().Value;
var build = serviceProvider.GetRequiredService>().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

https://github.com/dotnet/extensions/blob/00b5638b39eef97ed9184a113b082ff889a710bc/src/Libraries/Microsoft.Extensions.AmbientMetadata.Build/Microsoft.Extensions.AmbientMetadata.Build.csproj#L7

vs

https://github.com/dotnet/extensions/blob/00b5638b39eef97ed9184a113b082ff889a710bc/src/Libraries/Microsoft.Extensions.AmbientMetadata.Application/Microsoft.Extensions.AmbientMetadata.Application.csproj#L4

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.