Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Configuration for trimmed exe including SqlClient #2685

Unanswered
jods4 asked this question in Q&A
Discussion options

I understand MS.Data.SqlClient does not support trimming yet.
Could the team share a build configuration that works in .NET 8.0 to include SqlClient untrimmed into an exe that is otherwise trimmed?

My goal is to publish a CLI tool that is self-contained -- and obviously trimmed for size.

Since I updated to latest MS.Data.SqlClient and .NET 8.0, no configuration I tried to exclude SqlClient from trimming works.
The only setup that works is completely disabling trimming, and insead of being ~30Mo the self-contained executable is ~180Mo now. That's terrible for a CLI tool.

Most things I tried usually end up in a TypeLoadException with "Unable to load DLL 'Microsoft.Data.SqlClient.SNI.dll'".

You must be logged in to vote

Replies: 1 comment 5 replies

Comment options

You must be logged in to vote
5 replies
Comment options

Thanks! This project is interesting to remove bits that shouldn't be there in the first place, but that's not really my question.

I'm ok (for now) with including all of MS.Data.SqlClient in my application, without any kind of trimming for this lib.

I'd like to still be able to use the dotnet trimming feature for the rest of my app.

There are documented options here:
https://learn.microsoft.com/en-us/dotnet/core/deploying/trimming/trimming-options?pivots=dotnet-8-0

And I've tried to go back to the previous default of partial for TrimMode, I've tried rooting MS.Data.SqlClient with TrimmerRootAssembly but it didn't work.

I'd like an example of a csproj configuration where .net 8 trimming works in a project using SqlClient (understanding that SqlClient itself can't be trimmed, but the rest should).

Comment options

Hi,
Did you ever get anywhere with getting this to work?
I've been trying to get a trimmed self contained app working on .NET 9 and getting TypeInitializationExceptions when it does some SSL setup in TDSParser, and as you say, setting trim mode and rooting the SqlClient assembly doesn't help.

Marking System.Private.CoreLib as a root assembly does seem to help, but I haven't worked out exactly what it is that's getting trimmed when it shouldn't yet

Comment options

Found a small (~2MB) improvement here https://dlosch.github.io/dotnet/sql-server/aot/trimming/2025/08/22/SqlClient.html

<linker>
 <assembly fullname="System.Private.CoreLib">
 <type fullname="System.StubHelpers.InterfaceMarshaler" preserve="all"/>
 </assembly>
</linker>
Comment options

As of 7.0.0-preview1, it's technically possible to use RuntimeHostConfigurationOption to set the UseManagedNetworkingOnWindows AppContext switch. This will allow the IL trimmer to remove the unused SNI (whether this is managed or native.)

<RuntimeHostConfigurationOption Include="Switch.Microsoft.Data.SqlClient.UseManagedNetworkingOnWindows" value="true" />

Setting this to true will force SqlClient to use the managed SNI and remove all references to the native SNI, setting it to false will do the opposite.

With this being said: SqlClient doesn't officially support trimming. The RuntimeHostConfigurationOption approach is undocumented because it doesn't completely unlock the feature.

A handful of features aren't trim-compatible at all:

  • The native SNI isn't statically linked to the finished EXE (which also blocks untrimmed single-file publishing.)
  • Using System.Configuration to dynamically load a type based on app.config will be hit-and-miss at best. The use of System.Configuration can't be trimmed because it's the only way to define LocalDB instance versions.
  • User-defined CLR types don't work, and I don't think will be able to work without a new library API of some kind to register the type - SQL Server sends the type information as a string at runtime, long after the linker has done its work.
Comment options

@peterhirn @edwardneal Great tips to help with using MSSQL in small tools, thanks!

It feels like those limitations should be documented and we should be able to trim as long as we don't use LocalDB nor user-defined CLR types.

Blocking that scenario completely because a few edge-cases are not supported is sad.

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

AltStyle によって変換されたページ (->オリジナル) /