dotnet / aspnetcore

ASP.NET Core is a cross-platform .NET framework for building modern cloud-based web applications on Windows, Mac, or Linux.
https://asp.net
MIT License
35.46k stars 10.03k forks source link

ControllerFeatureProvider throws System.Reflection.ReflectionTypeLoadException: "Unable to load one or more of the requested types. Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolverFactory' #53134

Closed SeppPenner closed 10 months ago

SeppPenner commented 10 months ago

Is there an existing issue for this?

Describe the bug

When upgrading to Net8.0 and all the latest NuGet packages, the following error occurs in the ControllerFeatureProvider class:

System.Reflection.ReflectionTypeLoadException: "Unable to load one or more of the requested types.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolverFactory' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolver' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'."

The details are:

System.Reflection.ReflectionTypeLoadException
  HResult=0x80131602
  Nachricht = Unable to load one or more of the requested types.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolverFactory' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolver' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
  Quelle = System.Private.CoreLib
  Stapelüberwachung:
   bei System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
   bei Microsoft.AspNetCore.Mvc.Controllers.ControllerFeatureProvider.PopulateFeature(IEnumerable`1 parts, ControllerFeature feature) in /_/src/Mvc/Mvc.Core/src/Controllers/ControllerFeatureProvider.cs: Zeile22

grafik

The issue occurs when calling

app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

from the Startup.cs class.

Expected Behavior

There have been changes in https://github.com/npgsql/npgsql/issues/5438 that seem to cause the issue (TypeHandlerResolverFactory and TypeHandlerResolver is now internal). Not sure though why this is related.

Steps To Reproduce

I guess, that the problem occurs if a controller contains models (e.g. as return type) that have a reference to the Npgsql package somehow.

Exceptions (if any)

System.Reflection.ReflectionTypeLoadException: "Unable to load one or more of the requested types.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolverFactory' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'.
Could not load type 'Npgsql.Internal.TypeHandling.TypeHandlerResolver' from assembly 'Npgsql, Version=8.0.1.0, Culture=neutral, PublicKeyToken=5d8b90d52f46fda7'."

.NET Version

8.0.100

Anything else?

8.0.100

C:\Users\Nutzer>dotnet --info
.NET SDK:
 Version:           8.0.100
 Commit:            57efcf1350
 Workload version:  8.0.100-manifests.6a1e483a

Laufzeitumgebung:
 OS Name:     Windows
 OS Version:  10.0.22621
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100\

Installierte .NET-Workloads:
 Workload version: 8.0.100-manifests.6a1e483a
 [wasm-tools]
   Installationsquelle: VS 17.8.34330.188
   Manifestversion:    8.0.0/8.0.100
   Manifestpfad:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.workload.mono.toolchain.current\8.0.0\WorkloadManifest.json
   Installationstyp:              Msi

 [android]
   Installationsquelle: VS 17.8.34330.188
   Manifestversion:    34.0.43/8.0.100
   Manifestpfad:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.android\34.0.43\WorkloadManifest.json
   Installationstyp:              Msi

 [maui-windows]
   Installationsquelle: VS 17.8.34330.188
   Manifestversion:    8.0.3/8.0.100
   Manifestpfad:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maui\8.0.3\WorkloadManifest.json
   Installationstyp:              Msi

 [maccatalyst]
   Installationsquelle: VS 17.8.34330.188
   Manifestversion:    17.0.8478/8.0.100
   Manifestpfad:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.maccatalyst\17.0.8478\WorkloadManifest.json
   Installationstyp:              Msi

 [ios]
   Installationsquelle: VS 17.8.34330.188
   Manifestversion:    17.0.8478/8.0.100
   Manifestpfad:       C:\Program Files\dotnet\sdk-manifests\8.0.100\microsoft.net.sdk.ios\17.0.8478\WorkloadManifest.json
   Installationstyp:              Msi

Host:
  Version:      8.0.0
  Architecture: x64
  Commit:       5535e31a71

.NET SDKs installed:
  5.0.408 [C:\Program Files\dotnet\sdk]
  6.0.417 [C:\Program Files\dotnet\sdk]
  7.0.404 [C:\Program Files\dotnet\sdk]
  8.0.100 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.All 2.1.30 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.All]
  Microsoft.AspNetCore.App 2.1.30 [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.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.25 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 2.1.30 [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.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.15 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 3.1.32 [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.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.25 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.14 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  arm64 [C:\Program Files\dotnet]
    registered at [HKLM\SOFTWARE\dotnet\Setup\InstalledVersions\arm64\InstallLocation]
  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
BrennanConroy commented 10 months ago

Those types don't exist https://github.com/search?q=repo%3Anpgsql%2Fnpgsql%20TypeHandlerResolverFactory%20&type=code https://github.com/search?q=repo%3Anpgsql%2Fnpgsql+TypeHandlerResolver&type=code Not sure what you're expecting here.

SeppPenner commented 10 months ago

Those types don't exist https://github.com/search?q=repo%3Anpgsql%2Fnpgsql%20TypeHandlerResolverFactory%20&type=code https://github.com/search?q=repo%3Anpgsql%2Fnpgsql+TypeHandlerResolver&type=code Not sure what you're expecting here.

I know. And I don't know why I would need them either... After I upgraded from Net7 to Net8, the error occurs when I try to add the controllers.

What I expect? No error basically, as it's completely unclear where this comes from.

SeppPenner commented 10 months ago

My guess is the following:

First project: (Let's call it API) has controllers and references project 2 that has model classes. Second project: (Let's call it Models) has data models and references Npgsql.

--> When adding the controller routes with .MapControllers();, MVC somehow tries to access these type initializers. Why? No idea? Is it a bug in MVC or in Npgsql? No idea either.

I'm just looking for a fix or workaround how to handle this to be able to upgrade to Net8.

BrennanConroy commented 10 months ago

I'm guessing your second project is building with 7.0.x of npgsql and the first project is using 8.0.x of npgsql. 8.0 is being resolved when running the project and since there is a "breaking" change where those types don't exist anymore, you're getting that error. To fix it, either don't use internal types from npgsql, or update the second project to 8.0 (which will also prevent you from using the internal types).

SeppPenner commented 10 months ago

I'm guessing your second project is building with 7.0.x of npgsql and the first project is using 8.0.x of npgsql.

Npgsql is version 8.0.1 in all projects... That was the first thing I checked yesterday after it didn't work as expected.

Can I somehow easily check if another referenced NuGet package uses Npgsql in an older version?

martincostello commented 10 months ago

Do you have a NuGet package that's in your dependency tree somewhere that is compiled against the 7.0.x version? The {ProjectName}.deps.json file in the build output should contain the whole dependency graph.

SeppPenner commented 10 months ago

I guess, I found the issue...

Do you have a NuGet package that's in your dependency tree somewhere that is compiled against the 7.0.x version? The {ProjectName}.deps.json file in the build output should contain the whole dependency graph.

The issue is that a dependency of project 2 (aka Models) had a dependency to another project that had a dependency to Npgsql 7.11.0 or something...

So basically, the issue is dll / dependency hell 😅

Thanks @martincostello for pointing that out.