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

SqlClient Should Support localhost in Connection Strings on Linux #23307

Closed
RehanSaeed opened this issue Aug 23, 2017 · 5 comments
Closed

SqlClient Should Support localhost in Connection Strings on Linux #23307

RehanSaeed opened this issue Aug 23, 2017 · 5 comments
Labels
area-System.Data.SqlClient enhancement Product code improvement that does NOT require public API changes/additions os-linux Linux OS (any supported distro)
Milestone

Comments

@RehanSaeed
Copy link

When using a connection string containing Data Source=localhost on linux (in a Docker container in my case) I get the following error:

System.Data.SqlClient.SqlException occurred
  HResult=0x80131904
  Message=A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: TCP Provider, error: 35 - An internal exception was caught)
  Source=Core .Net SqlClient Data Provider
  StackTrace:
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, Object providerInfo, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, Boolean applyTransientFaultHandling)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection)
   at System.Data.ProviderBase.DbConnectionPool.PoolCreateRequest(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.QueueUserWorkItemCallbackDefaultContext.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
   at System.Threading.ThreadPoolWorkQueue.Dispatch()

Inner Exception 1:
AggregateException: One or more errors occurred. (This protocol version is not supported.)

Inner Exception 2:
NotSupportedException: This protocol version is not supported.

I believe localhost should be supported as it's a common scenario and to smooth out the platform inconsistencies. My understanding is that you must use an IP address on linux at the moment. I can't seem to get a working connection to the SQL Server installation on my local machine. My local SQL Server instance does not accept remote connections at the moment, I suspect this is why I cannot connect. I can however, point to a SQL Server public IP address that works.

As a workaround, I'm thinking I could programmatically replace localhost with the required IP address but I haven't been able to figure out how to do this. Here is a general idea of what I've tried:

private static string GetConnectionString(string connectionString)
{
    if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
    {
        var ipAddress = NetworkInterface
            .GetAllNetworkInterfaces()
            .Where(x =>
                x.NetworkInterfaceType == NetworkInterfaceType.Ethernet &&
                x.OperationalStatus == OperationalStatus.Up)
            .SelectMany(x => x.GetIPProperties().DnsAddresses)
            .FirstOrDefault();
        if (ipAddress != null)
        {
            connectionString = connectionString.Replace("localhost", ipAddress.ToString());
        }
    }

    return connectionString;
}
@RehanSaeed
Copy link
Author

I suspect this might be a Docker Tools for Visual Studio issue, so I've raised microsoft/DockerTools#40.

@saurabh500
Copy link
Contributor

localhost is supported on Linux. Have you forward the Sql Server port to localhost machine on Docker?

@RehanSaeed
Copy link
Author

RehanSaeed commented Feb 13, 2018

I should add that I'm not running SQL Server in a container, it's installed on my machine, the old fashioned way. I am able to use my SQL Server instance using localhost when running normally outside a container.

@saurabh500
Copy link
Contributor

@RehanSaeed I setup a Ubuntu 16.04 VM, installed Sql Server 2017 Express edition, and created a connection using SqlClient using localhost as the Server name.
I tried the following connection strings to connect to localhost and they all work

  1. Server=(local);uid=sa;pwd=Password
  2. Server=.;uid=sa;pwd=Password
  3. Server=localhost;uid=sa;pwd=Password

I am able to use my SQL Server instance using localhost when running normally outside a container.

Are you confirming that you are not facing an issue anymore and this can be closed?

@saurabh500
Copy link
Contributor

I had misunderstood that you are running Sql Server in docker. It is in fact the other way around where you are running the application with SqlClient in Docker.
Just to be clear, the Docker image doesn't run with the same IP address as the host. Hence localhost inside the docker image doesn't point to the host machine where your Sql Server is hosted. That is the reason you are not able to connect as a network path using the hostname "localhost" couldn't resolve to a Sql Server running on port 1433 in the same container.

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.1.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 20, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Data.SqlClient enhancement Product code improvement that does NOT require public API changes/additions os-linux Linux OS (any supported distro)
Projects
None yet
Development

No branches or pull requests

3 participants