Pavel K
Pavel K

Reputation: 3618

NullReferenceException inside .NET code of SqlConnection.CacheConnectionStringProperties()

I'm facing really strange issue. Given the code below:

static void Main()
{
    var c = new System.Data.SqlClient.SqlConnection();

    c.ConnectionString = "Data Source=SOME_NAME;Initial Catalog=SOME_DB;Integrated Security=True";
    c.ConnectionString = ""; //null also triggers exception

    Console.WriteLine("Success");
}

It worked fine for quite a time, but on newest version of Windows 10 (1803) which has .NET Version 4.7.03056 Release 461808 (seems to be 4.7.2) it crashes with following exception:

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object.
   at System.Data.SqlClient.SqlConnection.CacheConnectionStringProperties()
   at System.Data.SqlClient.SqlConnection.set_ConnectionString(String value)
   at TestCacheConnectionStringProperties.Program.Main()

This crashes on the second assignment, if I remove any of the assignments of the ConnectionString it works ok.

I've looked at the sources and didn't find a place where NullReferenceException could happen (however sources seems to be for .NET Framework 4.7.1 so smth may change).

Now the question is - what causes this issue? Is this a .NET bug? If yes - how to address it?

UPDATE: According to the comments - thanks very much guys - the issue is caused by these lines (decompiled):

private void CacheConnectionStringProperties()
{
  SqlConnectionString connectionOptions = this.ConnectionOptions as SqlConnectionString;
  if (connectionOptions != null)
    this._connectRetryCount = connectionOptions.ConnectRetryCount;

  //Seems like this is causing the bug because it is not inside of null-check-if for connectionOptions variable
  if (this._connectRetryCount != 1 || !ADP.IsAzureSqlServerEndpoint(connectionOptions.DataSource))
    return;
  this._connectRetryCount = 2;
}

It is somehow related to Azure and is quite different from what is available in sources.

I've posted the issue here and will wait for response.

Upvotes: 14

Views: 1839

Answers (1)

Saurabh Singh
Saurabh Singh

Reputation: 21

This is a documented known issue in System.Data in .NET Framework 4.7.2 https://github.com/Microsoft/dotnet/blob/master/releases/net472/dotnet472-known-issues.md

The issue is currently being addressed and will be available in future updates of .NET Framework 4.7.2

Upvotes: 2

Related Questions