0

I have .net 4.6.1 desktop application that migrates DB from 'old' version to the 'current' version.

It works perfectly fine when I test it locally. But I need to deploy the DB on Azure SQL Server and then migrate the DB. I wanted to do that from Azure VM but I get the following error:

System.Data.SqlClient.SqlException (0x80131904): A transport-level error has occurred when receiving results from the server. (provider: TCP Provider, error: 0 - An existing connection was forcibly closed by the remote host.) ---> System.ComponentModel.Win32Exception (0x80004005): An existing connection was forcibly closed by the remote host
   at System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData, DbConnectionPool pool, String accessToken, Boolean applyTransientFaultHandling, SqlAuthenticationProviderManager sqlAuthProviderManager)
   at System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.CreateNonPooledConnection(DbConnection owningConnection, DbConnectionPoolGroup poolGroup, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()

I tried the same from my local machine, accessing Azure SQL Server with my tool and it succeeded, but when I tried to do the same from Azure VM it failed. I tried with Win10, Win Server 2012 and Win Server 2016 VMs.

After some more attempts, I noticed that the issue was caused by using TransactionScope class.

My code is:

using (TransactionScope ts = new TransactionScope())
{
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
    connection.Open();
    // some stuff
    }
    ts.Complete();
}

Any ideas?

rraszewski
  • 1,135
  • 7
  • 21
  • As a workaround you could use `connection.BeginTransaction()` – NotFound Apr 10 '19 at 13:49
  • I don't want to modify an existing solution since it works fine for other cases. And as far as I know, `TransactionScope` supports multiple transactions. – rraszewski Apr 10 '19 at 14:13
  • From the error "connection was forcibly closed by the remote host", you can check the Azure DB if it allows the access to Azure services or allows all the IPs to access it. – Charles Xu Apr 11 '19 at 08:00
  • yes, it allows. If I remove `TransactionScope' lines everything works fine. I can also do some basic queries without transactions. So I can connect to the DB. But I cannot use TransactionScope... – rraszewski Apr 11 '19 at 10:15

0 Answers0