You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Raise exception on calling SqlClientFactory.Instance from facade assembly #4081
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.
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.
Activity
guidol-ive commentedon Mar 24, 2026
Possibly problems caused by returning the somewhat vague `nul`` instead of an actionable error:
priyankatiwari08 commentedon Mar 31, 2026
Thank you for reporting this. I'm looking into this issue and will follow up with a fix.
priyankatiwari08 commentedon Sep 17, 2026
/triage
github-actions commentedon Sep 17, 2026
🔍 Triage Summary (on-demand re-triage)
Area\NetcoreSqlClientFactory.Instancenull behavior)Analysis
The issue reports that when the
Microsoft.Data.SqlClientfacade assembly (the stub shipped for the top-level package reference) fails to redirect to the correctruntimes/<rid>implementation assembly — e.g., becausedeps.jsonis missingruntimeTargetsforMicrosoft.Data.SqlClient— callingSqlClientFactory.Instance.CreateConnection()throws an unhelpfulNullReferenceExceptioninstead of the expectedPlatformNotSupportedExceptionthat other facade members throw. The likely affected code lives in the facade/type-forwarding andSqlClientFactorystatic initialization path (SqlClientFactory.cs,TypeForwards.netcore.cs), whereInstanceis a field that can silently remainnullin the facade rather than being validated at access time. Given this causes silent/confusing failures for consumers with incompletedeps.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.Instancefrom 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, accessingInstancethrows a clear, actionable exception (e.g.,PlatformNotSupportedExceptionconsistent with the other facade stub methods) instead of allowing aNullReferenceExceptionto surface downstream inCreateConnection()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 exactdeps.jsonrepro steps given here.