Skip to content

Native AoT trim analysis warning : WithLinkGeneratorDecorator #1094

@Jflaurendeau

Description

@Jflaurendeau

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I was playing with native AoT and had a trim analysis warning IL2072.

ILC : Trim analysis warning IL2072: Microsoft.Extensions.DependencyInjection.IServiceCollectionExtensions.WithLinkGeneratorDecorator(IServiceCollection): 'implementationType' argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors' in call to 'Microsoft.Extensions.DependencyInjection.ServiceDescriptor.Describe(Type,Type,ServiceLifetime)'. The return value of method 'System.Object.GetType()' does not have matching annotations. The source value must declare at least the same requirements as those declared on the target location it is assigned to.

Steps To Reproduce

Package:

<ItemGroup>
  <PackageReference Include="Asp.Versioning.Http" Version="8.1.0" />
  <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.20.1" />
</ItemGroup>

Services used:

var builder = WebApplication.CreateSlimBuilder(args);

builder.Services.ConfigureHttpJsonOptions(options =>
{
    options.SerializerOptions.TypeInfoResolverChain.Insert(0, AppJsonSerializerContext.Default);
});
builder.Services.AddApiVersioning(options =>
{
    options.DefaultApiVersion = new ApiVersion(1);
    options.AssumeDefaultVersionWhenUnspecified = true;
    options.ApiVersionReader = ApiVersionReader.Combine(
        new UrlSegmentApiVersionReader(),
        new HeaderApiVersionReader("Api-Version"),
        new QueryStringApiVersionReader()); // default: ?api-version=X.Y
});
var app = builder.Build();

Exceptions (if any)

No response

.NET Version

8

Anything else?

I think this is related to the instance at line 177, used at line 181.

var decoratedType = descriptor switch
{
{ ImplementationType: var type } when type is not null => type,
{ ImplementationInstance: var instance } when instance is not null => instance.GetType(),
_ => throw new InvalidOperationException(),
};
services.Replace( Describe( decoratedType, decoratedType, lifetime ) );

It is just a warning and based on my code path, the ImplementationInstance branch of the switch expression should not be hit.
This might not be an issue or you may already know a work around, but I did not find any comment on this warning, so I though I should share it.

First time writing an item on an OSS project. I read the code of conduct and etiquette, but feel free to share other unwritten rules if this post should be improved.

Metadata

Metadata

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions