Thanks to visit codestin.com
Credit goes to github.com

Skip to content

How to publish an app using daily runtime packages? #84372

@eerhardt

Description

@eerhardt

I'm seeing the following exception from the ASP.NET benchmarks for PublishTrimmed/ReadyToRun/SingleFile:

Fatal error. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
   at System.RuntimeTypeHandle.RegisterCollectibleTypeDependency(System.RuntimeType, System.Reflection.RuntimeAssembly)
   at System.Reflection.TypeNameParser.GetTypeReferencedByCustomAttribute(System.String, System.Reflection.RuntimeModule)
   at System.Reflection.CustomAttributeTypedArgument..ctor(System.Reflection.RuntimeModule, System.Reflection.CustomAttributeEncodedArgument)
   at System.Reflection.RuntimeCustomAttributeData.get_ConstructorArguments()
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.GetDynamicallyAccessedMemberTypes(System.Type)
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.ValidateTrimmingAnnotations(System.Type, System.Type[], System.Type, System.Type[])
   at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteFactory.Populate()
   at Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(System.Collections.Generic.ICollection`1<Microsoft.Extensions.DependencyInjection.ServiceDescriptor>, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions)
   at Microsoft.Extensions.DependencyInjection.ServiceCollectionContainerBuilderExtensions.BuildServiceProvider(Microsoft.Extensions.DependencyInjection.IServiceCollection, Microsoft.Extensions.DependencyInjection.ServiceProviderOptions)
   at Microsoft.Extensions.Hosting.HostApplicationBuilder.Build()
   at Microsoft.AspNetCore.Builder.WebApplicationBuilder.Build()
   at Program.<Main>$(System.String[])

Minimal Repro instructions

  1. Using a recent .NET 8 SDK
  2. dotnet publish the following app:
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net8.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
    <PublishSingleFile>true</PublishSingleFile>
  </PropertyGroup>

  <ItemGroup>
    <FrameworkReference Update="Microsoft.NETCore.App"
                        RuntimeFrameworkVersion="8.0.0-preview.4.23181.16" />
  </ItemGroup>
</Project>
using System.Diagnostics.CodeAnalysis;
using System.Reflection;

Console.WriteLine(GetDynamicallyAccessedMemberTypes(typeof(MyGenericType<>).GetGenericArguments()[0]));

static DynamicallyAccessedMemberTypes GetDynamicallyAccessedMemberTypes(Type serviceGenericType)
{
    foreach (CustomAttributeData attributeData in serviceGenericType.GetCustomAttributesData())
    {
        if (attributeData.AttributeType.FullName == "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMembersAttribute" &&
            attributeData.ConstructorArguments.Count == 1 &&
            attributeData.ConstructorArguments[0].ArgumentType.FullName == "System.Diagnostics.CodeAnalysis.DynamicallyAccessedMemberTypes")
        {
            return (DynamicallyAccessedMemberTypes)(int)attributeData.ConstructorArguments[0].Value!;
        }
    }

    return DynamicallyAccessedMemberTypes.None;
}

internal class MyGenericType<[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)] T1>
{
}
  1. .\bin\Release\net8.0\win-x64\publish\Net8Console.exe

Original Repro instructions

  1. Using a recent .NET 8 SDK
  2. git clone https://github.com/aspnet/Benchmarks
  3. cd benchmarks\src\BenchmarksApps\BasicMinimalApi
  4. Edit the BasicMinimalApi.csproj to ensure you are using a recent runtime version (I know that recent SDKs don't have the latest runtime currently):
  <ItemGroup>
    <FrameworkReference Update="Microsoft.NETCore.App"
                        RuntimeFrameworkVersion="8.0.0-preview.4.23181.16" />
  </ItemGroup>
  1. dotnet publish /p:PublishTrimmed=true /p:PublishReadyToRun=true /p:PublishSingleFile=true /p:TrimMode=full /p:EnableRequestDelegateGenerator=true
  2. bin\Release\net8.0\win-x64\publish\BasicMinimalApi.exe

Expected result

The app should run successfully

Actual result

The above exception crashes the app

Notes

From looking at the last passing run to the first failing run, this appears to start occurring between 60b4804...390c2d5.

Looking through the commit list above I believe this is caused by #84159, since that change deleted the unmanged TypeNameParser and the exception stacktrace contains System.Reflection.TypeNameParser.GetTypeReferencedByCustomAttribute. But I haven't verified that is the reason for this failure.

cc @jkotas @AaronRobinsonMSFT

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    Status

    No status

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions