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

Connection Issue to SQL Server on Linux with .NET 8.0 #26127

Open
VittorMe opened this issue Jan 3, 2025 · 0 comments
Open

Connection Issue to SQL Server on Linux with .NET 8.0 #26127

VittorMe opened this issue Jan 3, 2025 · 0 comments

Comments

@VittorMe
Copy link

VittorMe commented Jan 3, 2025

Problem Description
I’m experiencing an error while attempting to connect my .NET 8.0 application, running in a Linux environment, to a SQL Server database. The issue persists even with a valid connection string, and the code works fine in Windows environments.

Stack Trace:

at Microsoft.Data.Common.ADP.LocalMachineRegistryValue(String subkey, String queryvalue)
at Microsoft.Data.SqlClient.SqlConnectionString..ctor(String connectionString)
at Microsoft.Data.SqlClient.SqlConnectionFactory.CreateConnectionOptions(String connectionString, DbConnectionOptions previous)
at Microsoft.Data.ProviderBase.DbConnectionFactory.GetConnectionPoolGroup(DbConnectionPoolKey key, DbConnectionPoolGroupOptions poolOptions, `DbConnectionOptions&` userConnectionOptions)
at Microsoft.Data.SqlClient.SqlConnection.ConnectionString_Set(DbConnectionPoolKey key)
at Microsoft.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
at Microsoft.Data.SqlClient.SqlConnection..ctor(String connectionString)
at SqlServerConnectionManager.CreateConnection(String baseConnectionString)

Environment Configuration

  • Operating System: Linux (Docker Container)

  • .NET Version: 8.0

  • SQL Server Driver: Microsoft.Data.SqlClient

  • Connection String:

Server=<server_ip>,1433;Database=<database_name>;User Id=<username>;Password=<password>;TrustServerCertificate=True;MultipleActiveResultSets=true

Application Configuration

public static DbConnection CreateConnection(string baseConnectionString)
{
    var parts = baseConnectionString.Split(';')
        .Where(p => !string.IsNullOrWhiteSpace(p))
        .Select(p => p.Trim())
        .ToDictionary(
            p => p.Split('=')[0].Trim(),
            p => p.Split('=')[1].Trim(),
            StringComparer.OrdinalIgnoreCase
        );

    var finalConnectionString = string.Join(";", new[]
    {
        $"Server={parts["Server"]}",
        $"Database={parts["Database"]}",
        $"User Id={parts["UID"]}",
        $"Password={parts["PWD"]}",
        "TrustServerCertificate=True",
        "Encrypt=False",
        "MultipleActiveResultSets=true",
        "Pooling=true",
        "Min Pool Size=1",
        "Max Pool Size=100",
        "Connect Timeout=30",
        "Application Name=VitRequested"
    });

    return new SqlConnection(finalConnectionString);
}


builder.Services.AddDbContext<DeliverySystemContext>(options =>
{
    var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");
    options.UseSqlServer(SqlServerConnectionManager.CreateConnection(connectionString),
        sqlServerOptions =>
        {
            sqlServerOptions.EnableRetryOnFailure(
                maxRetryCount: 5,
                maxRetryDelay: TimeSpan.FromSeconds(30),
                errorNumbersToAdd: null);
            sqlServerOptions.CommandTimeout(30);
        });
});

Attempts to Resolve:

1. Environment Configuration

  • Set DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1 in the Dockerfile.

  • Added the following switches:

AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.DisableUseOfAlias", true);
AppContext.SetSwitch("Switch.Microsoft.Data.SqlClient.UseLegacyNetworkProtocol", true);

2. Local Testing:

Code runs without issues in Windows environments.
Connection tested and verified using SQLCMD in the Linux environment.

3. Direct DbContext Tests:
A simple test class using Database.CanConnect() successfully connects in the Linux environment.

Expected Behavior
The application should connect to the SQL Server database without errors on Linux using Microsoft.Data.SqlClient.

Actual Behavior
The connection fails with an error related to the ADP.LocalMachineRegistryValue method, which seems to be querying the Windows Registry, a feature unavailable in Linux environments.

Request for Assistance

  • Is there any additional configuration required for Microsoft.Data.SqlClient in .NET 8.0 to work on Linux?

  • Any suggestions for fixes or alternative approaches?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant