Search code examples
c#.netsqlclient

Microsoft.Data.SqlClient - Error pre-login handshake


I'm trying to use the Microsoft.Data.SqlClient but cannot get the connection to work. However when I use the System.Data.SqlClient everything is ok.

try
{
    using (var connection = new SC.SqlConnection(connectionString))
    {
        connection.Open();

        connection.State.ShouldBe(ConnectionState.Open);
    }
}
catch (Exception e)
{
    throw;
}

The connection string is:

"server=[[**SERVER_NAME**]];Trusted_Connection=True;database=[[**DATABASE**]];min pool size=0;max pool size=100;Application Name=Data.Tests.DotNet;timeout=120"

The error that I am getting is:

Microsoft.Data.SqlClient.SqlException
A connection was successfully established with the server, but then an error occurred during the pre-login handshake. (provider: HTTP Provider, error: 0 - )

Everything that I can find show some message after the 'error: 0 -'.

Can anyone help?

UPDATE

The company's environments are:

  1. Dev / UAT - Ms Sql Server 2012 Enterprise (On-Prem)
  2. Prod - Ms Sql Server 2016 Azure (Cloud)

Connection Strings:

  1. Dev / UAT:
"server=[[**SERVER_NAME**]];Trusted_Connection=True;database=[[**DATABASE**]];min pool size=0;max pool size=100;Application Name=Data.Tests.DotNet;timeout=120"
  1. Production:
"Server=tcp:[[**SERVER_NAME]],1433;database=[[**DATABASE**]];Persist Security Info=False;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication='Active Directory Integrated';Application Name=Data.Tests.DotNet;"

Solution

  • Adding "TrustServerCertificate=true" to the end of connection string may resolve the issue.