"Could not instantiate object Microsoft.SqlServer.Management.Smo.AvailableSqlServers" after .NET 6.0 upgrade
Asked Answered
K

1

3

We recently switched from Visual Studio 2019 to 2022 and upgraded our C# projects from .NET Framework 4.5.2 to .NET 6.0 using the upgrade assistant. The upgrades were successful, though we've encountered a bunch of build and runtime errors, which we expected we would need to take care of. However, this one has stumped me.

Before the upgrade, the following code worked just fine:

using Microsoft.SqlServer.Management.Smo;
...
DataTable servers = SmoApplication.EnumAvailableSqlServers(false);

But following the update to 6.0, it started throwing a FailedOperationException. When I drill down, the innermost exception said Could not load type 'System.Data.Sql.SqlDataSourceEnumerator' from assembly 'System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

I tried using nuget to add the Microsoft.SqlServer.SqlManagementObjects package to the project. But it still throws a FailedOperationException, though this time the innermost exception says could not instantiate object Microsoft.SqlServer.Management.Smo.AvailableSQLServers. This message isn't really helpful, because I don't know what it's trying to instantiate (since EnumAvailableSqlServers is a static method) or why it can't instantiate it.

Before the upgrade, the project was referencing a local copy of Microsoft.SqlServer.Smo.dll. I removed it after adding the SqlManagementObjects package, but this had no effect.

I've searched for answers online but haven't yet found anything helpful. Any ideas of what to check?

Kerry answered 17/8, 2022 at 19:45 Comment(0)
K
2

I found this issue on github that looks to be the same one I'm having, and unfortunately it looks like EnumAvailableSqlServers just won't work in .NET 6.0 because it uses System.Data.Sql.SqlDataSourceEnumerator.Instance.GetDataSources() which does not exist in NetCore. That would explain the error I'm seeing. So I need to look for an alternative way to get available SQL servers that is compatible with .NET 6.0.

Kerry answered 18/8, 2022 at 16:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.