dotnet / dotnet/SqlClient

Raise exception on calling `SqlClientFactory.Instance` from facade assembly

Open
#4,081 2 comments 0 reactions 1 assignee Claimed by @priyankatiwari08 View on GitHub
Dominant language
C#
Stars
989
Forks
340
Avg merge
4d 18h
Merged PRs (30d)
69

Description

### 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

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.