Skip to content

Runtime assemblies contain invalid type forwarders #82169

@ltrzesniewski

Description

@ltrzesniewski

Description

Some runtime assemblies contain type forwarders to other assemblies which are not shipped with the SDK.

I noticed this while building a dependency graph of assemblies referenced by my project. System.IO.Ports couldn't be found, but it was referenced by System.

Reproduction Steps

Run this program, it's a bit sloppy (as it uses Assembly.LoadFrom and assumes the file name and assembly name are equal), but it demonstrates the point:

using System;
using System.IO;
using System.Linq;
using System.Reflection;

namespace ConsoleApp;

public static class Program
{
    public static void Main()
    {
        var dir = Path.GetDirectoryName(typeof(object).Assembly.Location)!;

        var assemblies = Directory.GetFiles(dir)
                                  .Where(f => f.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                                  .Select(Path.GetFileNameWithoutExtension)
                                  .ToHashSet();

        foreach (var assembly in assemblies.Order(StringComparer.OrdinalIgnoreCase))
        {
            try
            {
                var asm = Assembly.LoadFrom(Path.Combine(dir, $"{assembly}.dll"));

                foreach (var refAsmName in asm.GetReferencedAssemblies())
                {
                    if (!assemblies.Contains(refAsmName.Name))
                        Console.WriteLine($"{assembly} -> {refAsmName.Name}");
                }
            }
            catch
            {
                // Native dll
            }
        }
    }
}

Expected behavior

This program should produce no output. All references within the SDK should be resolved.

Actual behavior

The following output is produced with .NET 7.0.3:

mscorlib -> System.Security.Permissions
mscorlib -> System.Threading.AccessControl
System -> System.CodeDom
System -> Microsoft.Win32.SystemEvents
System -> System.Configuration.ConfigurationManager
System -> System.Diagnostics.PerformanceCounter
System -> System.Diagnostics.EventLog
System -> System.Security.Permissions
System -> System.IO.Ports
System -> System.Windows.Extensions
System -> System.Threading.AccessControl
System.Configuration -> System.Configuration.ConfigurationManager
System.Configuration -> System.Security.Permissions
System.Core -> System.Diagnostics.EventLog
System.Core -> System.Diagnostics.PerformanceCounter
System.Data -> System.Data.SqlClient
System.Data -> System.Security.Permissions
System.Data -> System.Data.Odbc
System.Data -> System.Data.OleDb
System.Drawing -> System.Drawing.Common
System.Drawing -> System.Configuration.ConfigurationManager
System.Drawing -> System.Security.Permissions
System.Net -> System.Security.Permissions
System.Runtime.Serialization -> System.Runtime.Serialization.Schema
System.Security -> System.Security.Cryptography.Pkcs
System.Security -> System.Security.Cryptography.ProtectedData
System.Security -> System.Windows.Extensions
System.Security -> System.Security.Cryptography.Xml
System.Security -> System.Security.Permissions
System.ServiceModel.Web -> System.ServiceModel.Syndication
System.ServiceProcess -> System.ServiceProcess.ServiceController
System.ServiceProcess -> System.Security.Permissions
System.Transactions -> System.Security.Permissions
WindowsBase -> System.IO.Packaging
WindowsBase -> System.Security.Permissions

The format is assembly -> referenced assembly, where referenced assembly couldn't be resolved.

All of the assemblies listed on the left of the -> contain only type forwarders.

Regression?

No response

Known Workarounds

No response

Configuration

dotnet --info
.NET SDK:
 Version:   7.0.103
 Commit:    276c71d299

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\7.0.103\

Host:
  Version:      7.0.3
  Architecture: x64
  Commit:       0a2bda10e8

.NET SDKs installed:
  3.1.426 [C:\Program Files\dotnet\sdk]
  5.0.403 [C:\Program Files\dotnet\sdk]
  5.0.405 [C:\Program Files\dotnet\sdk]
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.203 [C:\Program Files\dotnet\sdk]
  6.0.309 [C:\Program Files\dotnet\sdk]
  7.0.100-rc.2.22477.23 [C:\Program Files\dotnet\sdk]
  7.0.101 [C:\Program Files\dotnet\sdk]
  7.0.102 [C:\Program Files\dotnet\sdk]
  7.0.103 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.0-rc.2.22476.2 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.12 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.0-rc.2.22472.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.21 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.22 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 3.1.32 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.10 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.12 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 5.0.17 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.0-rc.2.22472.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.3 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  x86   [C:\Program Files (x86)\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\x86\InstallLocation]

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Infrastructure-librariesquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions