dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
15.37k stars 4.75k forks source link

Don't native build for the app cause missing meta exception #74503

Closed Jaafar-abusair closed 2 years ago

Jaafar-abusair commented 2 years ago

Description

Hi,

I'm trying to build application using ILComplier with IdentityServer4 package, when I run the application I got the following error when i run the application after the build

System.Reflection.MissingMetadataException: 'IdentityServer4.Configuration.DependencyInjection.Decorator<Microsoft.AspNetCore.Authentication.IAuthenticationService,Microsoft.AspNetCore.Authentication.AuthenticationService>' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859
   at identity12.autofarmer.net!<BaseAddress>+0x3f6628
   at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderExtensionsCore.AddDecorator[TService](IServiceCollection) + 0x2a9
   at Microsoft.Extensions.DependencyInjection.IdentityServerBuilderExtensionsCore.AddCookieAuthentication(IIdentityServerBuilder) + 0xa6
   at Microsoft.Extensions.DependencyInjection.IdentityServerServiceCollectionExtensions.AddIdentityServer(IServiceCollection) + 0x33
   at Program.<Main>$(String[]) + 0xa4

I tried to add the following to rd.xml, and I got another error on build this time

<Assembly Name="Microsoft.AspNetCore.Authentication">
      <Type Name="Microsoft.AspNetCore.Authentication.IAuthenticationService" Dynamic="Required All" />
      <Type Name="Microsoft.AspNetCore.Authentication.AuthenticationService" Dynamic="Required All" />
    </Assembly>

    <Assembly Name="IdentityServer4">
      <Type Name="IdentityServer4.Configuration.DependencyInjection.Decorator`1[[Microsoft.AspNetCore.Authentication.IAuthenticationService,Microsoft.AspNetCore.Authentication.AuthenticationService]]" Dynamic="Required All" />
    </Assembly>

Error:

EXEC : error : Failed to load type 'Microsoft.AspNetCore.Authentication.IAuthenticationService' from assembly 'Microsoft.AspNetCore
.Authentication, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' [E:\Clients\Native-Build\Native-Build\identity1
2.autofarmer.net\identity12.autofarmer.net.csproj]
  Internal.TypeSystem.TypeSystemException+TypeLoadException: Failed to load type 'Microsoft.AspNetCore.Authentication.IAuthenticati
  onService' from assembly 'Microsoft.AspNetCore.Authentication, Version=6.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'
     at Internal.TypeSystem.ThrowHelper.ThrowTypeLoadException(ExceptionStringID id, String typeName, String assemblyName)
     at Internal.TypeSystem.ResolutionFailure.Throw()
     at Internal.TypeSystem.Ecma.EcmaModule.GetType(String nameSpace, String name, NotFoundBehavior notFoundBehavior)
     at Internal.TypeSystem.ModuleDesc.GetType(String nameSpace, String name, Boolean throwIfNotFound)
     at Internal.TypeSystem.CustomAttributeTypeNameParser.ResolveCustomAttributeTypeDefinitionName(String name, ModuleDesc module,
  Boolean throwIfNotFound)
     at Internal.TypeSystem.CustomAttributeTypeNameParser.GetTypeByCustomAttributeTypeName(ModuleDesc module, String name, Boolean
  throwIfNotFound, Func`4 resolver)
     at ILCompiler.RdXmlRootProvider.ProcessTypeDirective(IRootingServiceProvider rootProvider, ModuleDesc containingModule, XEleme
  nt typeElement)
     at ILCompiler.RdXmlRootProvider.ProcessAssemblyDirective(IRootingServiceProvider rootProvider, XElement assemblyElement)
     at ILCompiler.RdXmlRootProvider.AddCompilationRoots(IRootingServiceProvider rootProvider)
     at ILCompiler.Compilation..ctor(DependencyAnalyzerBase`1 dependencyGraph, NodeFactory nodeFactory, IEnumerable`1 compilationRo
  ots, ILProvider ilProvider, DebugInformationProvider debugInformationProvider, DevirtualizationManager devirtualizationManager, I
  InliningPolicy inliningPolicy, Logger logger)
     at ILCompiler.ILScannerBuilder.ToILScanner()
     at ILCompiler.Program.Run(String[] args)
     at ILCompiler.Program.Main(String[] args)
C:\Users\Joe\.nuget\packages\microsoft.dotnet.ilcompiler\7.0.0-preview.4.22229.4\build\Microsoft.NETCore.Native.targets(276,5): err
or MSB3073: The command ""C:\Users\Joe\.nuget\packages\runtime.win-x64.microsoft.dotnet.ilcompiler\7.0.0-preview.4.22229.4\tools\\i
lc" @"obj\x64\Release\win-x64\native\identity12.autofarmer.net.ilc.rsp"" exited with code 1. [E:\Clients\Native-Build\Native-Build\
identity12.autofarmer.net\identity12.autofarmer.net.csproj]

Reproduction Steps

<TargetFramework>net6.0</TargetFramework> packages\identityserver4\4.1.2 packages\microsoft.aspnetcore.authentication\2.2.0

publish command dotnet publish [projectname] -c Release -r win-x64

Expected behavior

The app get build and run

Actual behavior

before updating the rd.xml, it get build and error on run time after updating the rd.xml, the error on build

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

dotnet-issue-labeler[bot] commented 2 years ago

I couldn't figure out the best area label to add to this issue. If you have write-permissions please help me learn by adding exactly one area label.

AaronRobinsonMSFT commented 2 years ago

@Jaafar-abusair I think this is a similar issue to https://github.com/dotnet/runtime/issues/74380.

See Microsoft.AspNetCore.Authentication, Version=6.0.0.0 and microsoft.dotnet.ilcompiler\7.0.0-preview.4.22229.4. Mixing 6 and 7 seem to be not supported.

/cc @MichalStrehovsky

MichalStrehovsky commented 2 years ago

This is just the RD.XML parser telling you in a very user-unfriendly way that the type IAuthenticationService doesn't exist in Microsoft.AspNetCore.Authentication. It's probably in a different assembly. #64869 tracks fixing the experience. It's in Future milestone because we don't plan to officially document RD.XML anywhere. Latest builds no longer have the fwlink pointing to those docs.

Jaafar-abusair commented 2 years ago

I have switched to ILCompiler 6, but the problem still there, I think it's because the IdentityServer4 is using Microsoft.AspNetCore.Authentication 2.2.0

is IdentityServer4 supported by native build?

MichalStrehovsky commented 2 years ago

The RD.XML was the correct approach, but you specified the wrong assembly - that's why it wasn't compiling.

Jaafar-abusair commented 2 years ago

@MichalStrehovsky can you guide me to the correct assembly?

jkotas commented 2 years ago

IAuthenticationService lives in Microsoft.AspNetCore.Authentication.Abstractions

jkotas commented 2 years ago

I found it by searching aspnetcore repo: https://github.com/dotnet/aspnetcore/blob/main/src/Http/Authentication.Abstractions/src/IAuthenticationService.cs

Jaafar-abusair commented 2 years ago

@jkotas yes, but the name space still "Microsoft.AspNetCore.Authentication", I think the problem related to assembly version, how to tell RD.XML file to load specific version?

jkotas commented 2 years ago

Versions do not matter in rd.xml. The aot compiler always loads the one and only version that the application was published with.

MichealDanida commented 2 years ago

I'm trying to build application using ILComplier with IdentityServer4 package, when I run the application I got the following error when running the application after the build .. 'IdentityServer4.Configuration.DependencyInjection.Decorator<Microsoft.AspNetCore.Authentication.IAuthenticationService,Microsoft.AspNetCore.Authentication.AuthenticationService>' is missing metadata. For more information, please visit http://go.microsoft.com/fwlink/?LinkID=392859 .. using Net 7 used rd.xml reference: PackageReference Include="AspNetCore.Identity.MongoDbCore" Version="3.1.2" /> PackageReference Include="IdentityServer4" Version="4.1.2" /> PackageReference Include="IdentityServer4.AccessTokenValidation" Version="3.0.1" /> PackageReference Include="IdentityServer4.AspNetIdentity" Version="4.1.2" /> PackageReference Include="Microsoft.AspNetCore.Authentication" Version="2.2.0" /> PackageReference Include="Microsoft.AspNetCore.Authentication.Abstractions" Version="2.2.0" /> PackageReference Include="Microsoft.AspNetCore.Authentication.Core" Version="2.2.0" /> PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="6.0.8" /> PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.8" /> PackageReference Include="Newtonsoft.Json" Version="13.0.1" /> PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />

Could you help me? michealdanida@gmail.com