Skip to content

SqlDependency.Start throws NRE against Azure SQL Database #1264

@ascott18

Description

@ascott18

Describe the bug

Calling SqlDependency.Start against an Azure SQL Database produces a NRE, rather than SQL.SqlDependencyDatabaseBrokerDisabled()

This is because the query to detect the service broker returns zero rows on Azure SQL, rather than a single row with a boolean. SqlDependencyListener is casting (bool)null.

Exception message:
System.NullReferenceException: 'Object reference not set to an instance of an object.'
Stack trace:
   at System.Runtime.CompilerServices.CastHelpers.Unbox(Void* toTypeHnd, Object obj)
   at SqlDependencyProcessDispatcher.SqlConnectionContainer..ctor(SqlConnectionContainerHashHelper hashHelper, String appDomainKey, Boolean useDefaults) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs:line 103
   at SqlDependencyProcessDispatcher.Start(String connectionString, String& server, DbConnectionPoolIdentity& identity, String& user, String& database, String& queueService, String appDomainKey, SqlDependencyPerAppDomainDispatcher dispatcher, Boolean& errorOccurred, Boolean& appDomainStart, Boolean useDefaults) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs:line 1592
   at SqlDependencyProcessDispatcher.Start(String connectionString, String queue, String appDomainKey, SqlDependencyPerAppDomainDispatcher dispatcher) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependencyListener.cs:line 1528
   at Microsoft.Data.SqlClient.SqlDependency.Start(String connectionString, String queue, Boolean useDefaults) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependency.cs:line 489
   at Microsoft.Data.SqlClient.SqlDependency.Start(String connectionString, String queue) in /_/src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/SqlDependency.cs:line 394

To reproduce

string connectionString = "<any azure sql database>"
SqlDependency.Start(connectionString);

Expected behavior

A deliberate InvalidOperationException is thrown, not an unhandled NRE.

Further technical details

Microsoft.Data.SqlClient version: 3.0.0
.NET target: 5.0
SQL Server version: Azure SQL
Operating system: Windows 10

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions