Skip to content

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

Open

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

Activity

  1. moved this from To triage to Investigating in SqlClient Boardon Mar 24, 2026
  2. priyankatiwari08 commented on Mar 31, 2026

    @priyankatiwari08
    Contributor

    Thank you for reporting this. I'm looking into this issue and will follow up with a fix.

  3. priyankatiwari08 commented on Sep 17, 2026

    @priyankatiwari08
    Contributor

    /triage

  4. github-actions commented on Sep 17, 2026

    @github-actions

    🔍 Triage Summary (on-demand re-triage)

    Check Result
    Issue type Bug
    Environment All required environment details provided for investigation
    Area Area\Netcore
    Duplicates Potentially related: #2064 (also linked by another commenter as a likely root cause of confusing SqlClientFactory.Instance null behavior)
    Regression Not indicated — this is a longstanding design gap in the facade/runtime-selection assembly, not a version regression

    Analysis

    The issue reports that when the Microsoft.Data.SqlClient facade assembly (the stub shipped for the top-level package reference) fails to redirect to the correct runtimes/<rid> implementation assembly — e.g., because deps.json is missing runtimeTargets for Microsoft.Data.SqlClient — calling SqlClientFactory.Instance.CreateConnection() throws an unhelpful NullReferenceException instead of the expected PlatformNotSupportedException that other facade members throw. The likely affected code lives in the facade/type-forwarding and SqlClientFactory static initialization path (SqlClientFactory.cs, TypeForwards.netcore.cs), where Instance is a field that can silently remain null in the facade rather than being validated at access time. Given this causes silent/confusing failures for consumers with incomplete deps.json (a known recurring support pain point per linked issues/Stack Overflow threads), this is a moderate-priority (P2) diagnosability bug — no data loss or security impact, but repeatedly causes support friction.

    Next Steps

    Recommend assigning this issue to Copilot coding agent to investigate converting SqlClientFactory.Instance from a plain field to a property (or adding a static constructor / null-check guard) so that when the facade assembly is loaded without successful redirection to the runtime-specific implementation, accessing Instance throws a clear, actionable exception (e.g., PlatformNotSupportedException consistent with the other facade stub methods) instead of allowing a NullReferenceException to surface downstream in CreateConnection() and other factory methods. Also review the linked #2064 and referenced Stack Overflow/blog threads, which describe the same root cause from slightly different angles (assembly load context issues), to ensure the fix addresses the general class of "facade loaded without runtime redirection" scenarios rather than only the exact deps.json repro steps given here.

    Note: This triage summary is auto-generated by an AI agent. The analysis and suggestions above have not been verified by a human maintainer. Please treat as preliminary guidance only.

    Generated by SqlClient Issue Auto-Triage for #4081 · copilot · auto · 85 AIC · ⌖ 13.1 AIC · ⊞ 11.9K · ◷

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions