Tadas Šukys
Tadas Šukys

Reputation: 4220

Microsoft.Data.SqlClient 5.1.2 throws NullReference exception, stack trace points to reference assembly

My Cloud Service worker role (.NET 4.8) references NuGet package Microsoft.Data.SqlClient version 5.1.2. The code works fine locally. Microsoft DevOps pipeline builds and publishes Cloud Service artifact .cspkg package (msbuild /t:publish /p:OverwriteReadOnlyFiles=true /p:IsPackaging=True ...) then the artifact is uploaded to Azure Cloud Service (extended support) resource.

Now, at the runtime on the Azure Cloud Service I get two exceptions System.NullReferenceException with these stack traces:

System.NullReferenceException:+Object+reference+not+set+to+an+instance+of+an+object.
at Microsoft.Data.SqlClient.SqlCommand.ExecuteReader()+in+D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netfx\ref\Microsoft.Data.SqlClient.cs:line+636

Unexpected+exception+in+AddBenchmarkJobRecord,+details=[System.NullReferenceException:+Object+reference+not+set+to+an+instance+of+an+object.
 at Microsoft.Data.SqlClient.SqlConnection.CreateCommand()+in+D:\a\_work\1\s\src\Microsoft.Data.SqlClient\netfx\ref\Microsoft.Data.SqlClient.cs:line+851

It seems interesting that stack traces point to reference assembly of Microsoft.Data.SqlClient. It seems that runtime loads reference assembly and executes it, that explains NullReferenceExceptions. RDP to the Azure Cloud Service resource and among assembly files I also find Microsoft.Data.SqlClient.dll reference assembly (only 72KB, verified with a disassembler): enter image description here

I found that reference assembly file Microsoft.Data.SqlClient.dll is published by the DevOps pipeline. I was trying to reproduce that locally, but had no success - locally published .cspkg package contains the right assembly file: enter image description here

Questions:

Thank you.

Upvotes: 0

Views: 80

Answers (1)

Tadas Šukys
Tadas Šukys

Reputation: 4220

MsBuild version 16 (that comes with Visual Studio 2019) includes reference assembly file Microsoft.Data.SqlClient.dll to the .cspkg package. But MsBuild version 17 (that comes with Visual Studio 2022) includes the "correct" assembly file. The DevOps pipeline was configured to use build tools v16 (vsVersion '16.0', see task VSBuild@1). Locally I use Microsoft Visual Studio 2022 and msbuild v17. The issue is solved by updating DevOps pipeline to use vsVersion '17.0'

Upvotes: 0

Related Questions