-
Couldn't load subscription status.
- Fork 5.2k
Description
Description
When publishing a .NET project as a self-contained single-file application on the Linux platform, the resulting binary does not include the required libSystem.Net.Security.Native.so library. This issue is observed specifically when using Microsoft.Data.SqlClient or System.Data.SqlClient libraries and calling the SqlConnection.Open() method with Integrated_Security=true in the connection string.
Reproduction Steps
-
Write a program which uses the
Microsoft.Data.SqlClientorSystem.Data.SqlClientlibraries. -
Call the
SqlConnection.Open()method withIntegrated_Security=truein the connection string. -
Build the project:
dotnet publish --framework net6.0 --runtime linux-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:IncludeAllContentForSelfExtract=true -
Execute the published binary.
-
Encounter the following exception if:
libSystem.Net.Security.Native.sois not present in the program's directory or library path, or:- dotnet framework is not installed, which should not be necessary with self-contained applications.
Microsoft.Data.SqlClient.SqlException (0x80131904): The type initializer for 'NetSecurityNative' threw an exception.
Code sample:
- project.csproj
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>dotnet_test</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Data.SqlClient" Version="5.1.1" />
</ItemGroup>
</Project>
- Program.cs
using System;
using Microsoft.Data.SqlClient; // or System.Data.SqlClient
class Program
{
static void Main()
{
// Connection string with Integrated Security
string connectionString = "Server=myServerAddress;Database=myDatabase;Integrated Security=true;Encrypt=false;";
try
{
// Create a new SqlConnection using the connection string
using (SqlConnection connection = new SqlConnection(connectionString))
{
// Open the connection
connection.Open(); // Exception occurs here if libSystem.Net.Security.Native.so is missing
}
Console.WriteLine("Connection successful!");
}
catch (Exception ex)
{
Console.WriteLine($"Exception: {ex}");
}
}
}
Expected behavior
The published self-contained single-file binary should include the libSystem.Net.Security.Native.so library, ensuring the program's functionality without the need for manual copying of the library or the need for the dotnet framework to be installed on the local machine.
Actual behavior
Running the program results in the following exception:
Microsoft.Data.SqlClient.SqlException (0x80131904): The type initializer for 'NetSecurityNative' threw an exception.
Regression?
No response
Known Workarounds
The known workaround, which is unacceptable for self-contained single-file programs, is to ensure libSystem.Net.Security.Native.so is in the library path of the running program.
Configuration
No response
Other information
No response