Skip to content

Type load fails - cannot locate 'Microsoft.SqlServer.Server.SqlDataRecord' #9

Closed

Description

@danpaul88

Describe the bug

Upon replacing System.Data.SqlClient with the latest preview of Microsoft.Data.SqlClient (version 1.0.19128.1-Preview) I get the following type load exception upon starting my .NET Framework 4.7 application;

ReflectionTypeLoadException
  Unable to load one or more of the requested types. Retrieve the LoaderExceptions property for more information.

  LoaderExceptions (3x the same)
    Could not load type 'Microsoft.SqlServer.Server.SqlDataRecord' from assembly 'Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5'.

  Stack Track
     at System.Reflection.RuntimeModule.GetTypes(RuntimeModule module)
     at System.Reflection.Assembly.GetTypes()

I've verified that Microsoft.Data.SqlClient.dll is present in the output folder of the build (and System.Data.SqlClient.dll is not present). I've run a dotnet clean and dotnet restore to try and fix it to no avail.

To reproduce

Upgrade a .NET framework 4.7 application using System.Data.SqlClient to Microsoft.Data.SqlClient and run it

Expected behavior

It would run successfully

Further technical details

Microsoft.Data.SqlClient version: 1.0.19128.1-Preview
.NET target: net47
SQL Server version: not relevant to issue (not getting far enough to try connecting)
Operating system: Windows 10

Activity

  1. ErikEJ commented on May 13, 2019

    @ErikEJ
    Contributor

    Duplicate of #4 ?

  2. danpaul88 commented on May 13, 2019

    @danpaul88
    Author

    Potentially, this is a desktop application but it has some asp net core dependencies for a signalr client and a few other client side operations. Doesn't use aspnetcore.app though

    Also I've verified that system.data.sqlclient does not appear in the output folder nor the dependency tree in visual studio - and the exception doesn't indicate a conflict, simply a failure to load

  3. danpaul88 commented on May 13, 2019

    @danpaul88
    Author

    OK - I've done some digging and noticed that, at the point of the exception, it's trying to populate the catalog for a System.ComponentModel.Hosting.AssemblyCatalog

     	mscorlib.dll!System.Reflection.Assembly.GetTypes()	Unknown
    >	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.AssemblyCatalog.InnerCatalog.get()	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.AssemblyCatalog.GetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition)	Unknown
     	System.Core.dll!System.Linq.Enumerable.SelectManyIterator<System.ComponentModel.Composition.Primitives.ComposablePartCatalog, System.Tuple<System.ComponentModel.Composition.Primitives.ComposablePartDefinition, System.ComponentModel.Composition.Primitives.ExportDefinition>>(System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.ComposablePartCatalog> source, System.Func<System.ComponentModel.Composition.Primitives.ComposablePartCatalog, System.Collections.Generic.IEnumerable<System.Tuple<System.ComponentModel.Composition.Primitives.ComposablePartDefinition, System.ComponentModel.Composition.Primitives.ExportDefinition>>> selector)	Unknown
     	mscorlib.dll!System.Collections.Generic.List<System.Tuple<System.ComponentModel.Composition.Primitives.ComposablePartDefinition, System.ComponentModel.Composition.Primitives.ExportDefinition>>.InsertRange(int index, System.Collections.Generic.IEnumerable<System.Tuple<System.ComponentModel.Composition.Primitives.ComposablePartDefinition, System.ComponentModel.Composition.Primitives.ExportDefinition>> collection)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.AggregateCatalog.GetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.AggregateCatalog.GetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.CatalogExportProvider.InternalGetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition, out System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> exports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition, out System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> exports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.CatalogExportProvider.GetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition, out System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> exports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.AggregateExportProvider.GetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition, out System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> exports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.CompositionContainer.GetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.TryGetExportsCore(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition, out System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.Export> exports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ExportProvider.GetExports(System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ImportEngine.TryGetExports(System.ComponentModel.Composition.Hosting.ExportProvider provider, System.ComponentModel.Composition.Primitives.ComposablePart part, System.ComponentModel.Composition.Primitives.ImportDefinition definition, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportSubset(System.ComponentModel.Composition.Hosting.ImportEngine.PartManager partManager, System.Collections.Generic.IEnumerable<System.ComponentModel.Composition.Primitives.ImportDefinition> imports, System.ComponentModel.Composition.Hosting.AtomicComposition atomicComposition)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImportsStateMachine(System.ComponentModel.Composition.Hosting.ImportEngine.PartManager partManager, System.ComponentModel.Composition.Primitives.ComposablePart part)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ImportEngine.TrySatisfyImports(System.ComponentModel.Composition.Hosting.ImportEngine.PartManager partManager, System.ComponentModel.Composition.Primitives.ComposablePart part, bool shouldTrackImports)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.ImportEngine.SatisfyImportsOnce(System.ComponentModel.Composition.Primitives.ComposablePart part)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.Hosting.CompositionContainer.SatisfyImportsOnce(System.ComponentModel.Composition.Primitives.ComposablePart part)	Unknown
     	System.ComponentModel.Composition.dll!System.ComponentModel.Composition.AttributedModelServices.SatisfyImportsOnce(System.ComponentModel.Composition.ICompositionService compositionService, object attributedPart)	Unknown
     	MYAPP.exe!MYAPP.Bootstrapper.Initialize() Line 134	C#
    

    Looking in the object browser shows that Microsoft.Data.SqlClient library doesn't actually contain a class called SqlDataReader (looks like its in System.Data instead), so quite why the composition service has decided to look in the Microsoft.Data.SqlClient library for that type I'm not sure...

  4. danpaul88 commented on May 13, 2019

    @danpaul88
    Author

    Aha - I think I've figured it out

    So, when you compile a library in netstandard2 and reference Microsoft.Data.Server.SqlClient you get an instance of SqlDataRecord from;

    #region Assembly Microsoft.Data.SqlClient, Version=1.0.19128.1, Culture=neutral, PublicKeyToken=23ec7fc2d6eaa4a5
    // C:\Users\myname\.nuget\packages\microsoft.data.sqlclient\1.0.19128.1-preview\ref\netstandard2.0\Microsoft.Data.SqlClient.dll
    #endregion
    

    But when you use that same type in a net framework 4.7 application you instead get an instance of SqlDataRecord from;

    #region Assembly System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
    // C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.7\System.Data.dll
    #endregion
    

    So when you have an intermediate netstandard2 project library consuming Microsoft.Data.SqlClient and using the Microsoft.SqlServer.Server.SqlDataRecord and then a net47 library (or application) consuming both that library and, in turn, Microsoft.Data.SqlClient, they disagree over which library to look in for the implementation of Microsoft.SqlServer.Server.SqlDataRecord

    It's even more obvious when you take composition out of the loop since it generates a compiler error;

    Severity	Code	Description	Project	File	Line	Suppression State
    Error	CS1503	Argument 1: cannot convert from 'Microsoft.SqlServer.Server.SqlDataRecord' to 'Microsoft.SqlServer.Server.SqlMetaData'	MsDataSqlClientNet47 (net47)	C:\Users\myname\source\repos\MsDataSqlClientNet47\App.xaml.cs	19	Active
    

    I've attached a ZIP file with a repro of the above compiler error for your consideration

    MsDataSqlClient_Issue9Repo.zip

  5. David-Engel commented on May 13, 2019

    @David-Engel
    Contributor

    This is very related to #4. You are just seeing a different aspect of the issue. On the Framework side of M.D.SqlClient, the Microsoft.SqlServer.Server classes are not exposed publicly. You will need to keep a reference to System.Data.SqlClient to use them. The outcome of #4 will determine if you need to keep a reference to S.D.SqlClient for those classes long-term, or if they will be made available under a different namespace.

  6. danpaul88 commented on May 13, 2019

    @danpaul88
    Author

    I did wonder if that was the case once I'd figured out the root cause.

    The problem therein is that any net standard intermediate library (such as a NuGet package which happens to use M.D.SqlClient but not S.D.SqlClient) is incompatible with any net framework library or application trying to consume it since the intermediate library is compiled to expect the types which don't exist on the net framework side of the fence. This breaks the expectation that a net framework library or application can consume net standard libraries equal or lower than the version of net standard that the net framework version corresponds to.

    I'll keep an eye on #4 to see what the resolution is - in the meantime I'll mark this as closed (feel free to reopen if you'd rather keep it alive to track this aspect of the issue :) )

  7. added a commit that references this issue on Jun 25, 2026
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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions