Home

System.Data.SqlClient types not found in LP6

The following code writes "Failure" in LP6 using either .NET Core 3.1 or .NET 5, but it writes "Success" in a .NET Core console app both on Windows and on Linux. (It also writes "Success" in LP5 or a Framework console app.)

I know that the SqlClientFactory class technically lives in the System.Data.SqlClient assembly under .NET Core (and the System.Data assembly under Framework). But why is it that a console app can correctly resolve this, but LP cannot?

// using System.Data.SqlClient NuGet package
var aqn = "System.Data.SqlClient.SqlClientFactory, System.Data";
var type = Type.GetType(aqn);
Console.WriteLine(type == null ? "Failure" : "Success");

Comments

  • I see what's happening: the CLR resolves type-forwarded assemblies via the ALC of the forwarding type rather than the requesting type (or the contextual reflection context). This makes sense if the destination assembly is alongside the forwarding assembly, but in this case, System.Data is part of the runtime whereas System.Data.SqlClient (to which SqlClientFactory forwards) comes from a user-specified NuGet package. With a Console application, everything loads statically into the same ALC, so this problem goes unnoticed. But for an application like LINQPad, which loads assemblies dynamically, the forwarded type will try to resolve via the default ALC, which knows nothing about it.

    I could lodge this as an issue with the CLR, but the response is likely to be YMMV when it comes to compatibility with .NET Framework. Type.GetType("System.Data.SqlClient.SqlClientFactory, System.Data") is .NET Framework code, and the intention may be for this to work only partially in .NET Core (i.e., in static loading scenarios but not dynamic loading scenarios).

    In any case, there are few enough type forwards of this kind that I can use a backup resolver on the default ALC to handle them without causing other issues. I'll try and get this into 6.15.6 beta, in a week or so.

  • Is there a line or two of code that I could add to my LP scripts to get the type to resolve correctly? I tried loading the assemblies manually, and also using AssemblyLoadContext.Default.LoadFromAssemblyName(), but neither worked for me.

Sign In or Register to comment.