Skip to content

Raise exception on calling SqlClientFactory.Instance from facade assembly #4081

@PatrickHofman

Description

@PatrickHofman

Describe the bug

The Microsoft.Data.SqlClient.dll assembly that is copied to the output folder on a build contains stubs that throw a PlatformNotSupportedException with the text "Microsoft.Data.SqlClient is not supported on this platform." on all relevant methods. This is intended since the correct assembly and its dependencies are loaded from the runtimes folder.

However, if this assembly loading from the runtimes folder does not succeed, for example when the deps.json file omits the correct runtimeTargets, the facade assembly is used.

As said, the assembly usually throws an exception on all methods, but not on calling the SqlClientFactory.Instance field. Although this field is marked not to be null, it is when only loading the facade assembly.

This results in a NullReferenceException on calling SqlClientFactory.Instance.CreateConnection() and others.

To reproduce

  • Create a console application;
  • Add the Microsoft.Data.SqlClient NuGet package;
  • Add this line of code SqlClientFactory.Instance.CreateConnection();
  • Build;
  • Open the [app].deps.json file using Notepad and remove "runtimeTargets" under "Microsoft.Data.SqlClient/7.0.0";
  • Run the application;
  • A NullReferenceException is thrown.

Expected behavior

A clear exception, for example by converting Instance to a property and throw an exception, or throw the exception from a static constructor.

Further technical details

Microsoft.Data.SqlClient version: 7.0.0
.NET target: .NET 10.0

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

Status

Investigating

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions