Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing libSystem.Net.Security.Native.so when publishing self-contained single-file project on Linux #88481

Closed
julesroussel3 opened this issue Jul 6, 2023 · 6 comments

Comments

@julesroussel3
Copy link

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

  1. Write a program which uses the Microsoft.Data.SqlClient or System.Data.SqlClient libraries.

  2. Call the SqlConnection.Open() method with Integrated_Security=true in the connection string.

  3. Build the project: dotnet publish --framework net6.0 --runtime linux-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:IncludeAllContentForSelfExtract=true

  4. Execute the published binary.

  5. Encounter the following exception if:

    1. libSystem.Net.Security.Native.so is not present in the program's directory or library path, or:
    2. 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

@ghost ghost added the untriaged New issue has not been triaged by the area owner label Jul 6, 2023
@ghost
Copy link

ghost commented Jul 6, 2023

Tagging subscribers to this area: @DavoudEshtehari, @David-Engel, @JRahnama
See info in area-owners.md if you want to be subscribed.

Issue Details

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

  1. Write a program which uses the Microsoft.Data.SqlClient or System.Data.SqlClient libraries.

  2. Call the SqlConnection.Open() method with Integrated_Security=true in the connection string.

  3. Build the project: dotnet publish --framework net6.0 --runtime linux-x64 --self-contained true -p:PublishSingleFile=true -p:IncludeNativeLibrariesForSelfExtract=true -p:IncludeAllContentForSelfExtract=true

  4. Execute the published binary.

  5. Encounter the following exception if:

    1. libSystem.Net.Security.Native.so is not present in the program's directory or library path, or:
    2. 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

Author: julesroussel3
Assignees: -
Labels:

area-System.Data.SqlClient

Milestone: -

@teo-tsirpanis
Copy link
Contributor

The problem is that SqlClient depends on System.Net.Security.Native, an implementation detail of the .NET runtime that is not supported for external use (it cannot find it on single-file deployments because it is statically linked to the executable). It must be updated to use managed APIs in the System.Security namespace.

Until it is fixed, the best thing you can do is to not use single-file deployments here.

@filipnavara
Copy link
Member

Until it is fixed, the best thing you can do is to not use single-file deployments here.

The fix to SqlClient was actually merged very recently: dotnet/SqlClient#2063. I am not sure if it shipped, or whether it will be available on .NET 8 only or also .NET 7 (depends on how they multi-target the libs).

@JRahnama
Copy link
Member

JRahnama commented Jul 7, 2023

As @filipnavara mentioned the fix has been added recently and it will be available on the next preview release at the end of current month.

@David-Engel
Copy link
Contributor

To clarify, it can only be fixed against .NET 7+. The new authentication APIs in .NET that are required for SqlClient to support Integrated_Security=true on Linux were only made available starting with .NET 7 and we've only just recently targeted them in the latest code. Look for it in Microsoft.Data.SqlClient 5.2 Preview 3, currently targeting July 20. (The System.Data.SqlClient NuGet package is essentially EOL and won't get updates for something like this.)

@David-Engel David-Engel removed the untriaged New issue has not been triaged by the area owner label Jul 13, 2023
@David-Engel
Copy link
Contributor

Closing this issue as resolved in Microsoft.Data.SqlClient 5.2 preview 2 and won't fix in System.Data.SqlClient.

@ghost ghost locked as resolved and limited conversation to collaborators Aug 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants