Skip to content

Runtime assemblies contain invalid type forwarders #82169

Closed

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

Activity

  1. ghost added
    untriaged
    on Feb 15, 2023
  2. am11 commented on Feb 15, 2023

    @am11
    Member
  3. bartonjs commented on Feb 15, 2023

    @bartonjs
    Member

    All of the libraries on the left look to me to be the .NET Framework compatibility assemblies; and all the libraries on the right are things that got "demoted" from inbox (.NET Framework) to NuGet (.NET Core / .NET 5+).

    I believe they're currently striking the balance between "try to make many libraries built for .NET Framework just work" and "we don't need all of this all the time".

    Basically, those specific handful of libraries should be treated as special and not resolved for dependency analysis.

  4. ViktorHofer commented on Feb 15, 2023

    @ViktorHofer
    Member

    You can find the list of .NET Framework façade assemblies here: https://github.com/dotnet/runtime/tree/main/src/libraries/shims/src

    All the assemblies that you list above fall under that bucket and as @bartonjs already described, they mainly exist for backward compatibility. Essentially, they are in the product (shared framework and runtime pack) in order for .NET Framework assemblies to be loadable on modern .NET. While that's their purpose, there's no guarantee that this actually works in all cases and it's not a path that we recommend. Instead, if developers want to share code between .NET Framework and modern .NET, we recommend to either use .NET Standard or even better these days, multi-target (via TargetFrameworks property).

    With continuous intentional breaking changes every major release (obsoletions, API removals, etc.), these facades will likely not add much value going forward. We could even discuss if we want to remove them now with the given alternatives. cc @ericstj

    TL;DR if you walk the shared framework's dependency graph, make sure to ignore facades. Technically, netstandard.dll is also a façade but it only type forwards into the shared framework.

  5. added
    question
    on Feb 15, 2023
  6. ltrzesniewski commented on Feb 15, 2023

    @ltrzesniewski
    ContributorAuthor

    Ok, thanks! I figured these were the façade assemblies, but I opened this issue as I didn't expect them to reference non-shipped assemblies. Providing the lesser used parts as NuGet packages makes sense though. 👍

    (and indeed, the load failure showed there was a legacy .NET Framework library hiding in the project dependencies)

  7. ghost removed
    untriaged
    on Feb 15, 2023
  8. ghost locked as resolved and limited conversation to collaborators on Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Infrastructure-librariesquestion

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions