I'm recording this here as I was not able to find another reference to this error with the same cause.
Scenario;
- Attempting to list installed instances of SQL Server databases by calling the SQL Management Objects (SMO) library.
- SQL Server 2022 is installed
- Application had been using SQL Server 2016 SMO but references had been upgraded and binding redirects put in place.
When attempting to use the following code, it resulted in the error below.
SqlConnection connection = new SqlConnection(connBuilder.ConnectionString);
ServerConnection sc = new ServerConnection(connection);
Server server = new Server(sc);
// breaks here...
if (server.Databases.Contains(databaseName))
Error
Microsoft.SqlServer.Management.Sdk.Sfc.EnumeratorException: Failed to retrieve data for this request.
---> Microsoft.SqlServer.Management.Sdk.Sfc.InternalEnumeratorException: Microsoft.SqlServer.Management.Smo.SqlServer is not derived from EnumObject.
at Microsoft.SqlServer.Management.Sdk.Sfc.ObjectCache.LoadElement(ObjectLoadInfo oli)
at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetServerVersion(Urn urn, Object ci)
at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetObjectsFromCache(Urn urn, Object ci)
at Microsoft.SqlServer.Management.Sdk.Sfc.Environment.GetData(Request req, Object ci)
at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.GetData(Object connectionInfo, Request request)
at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.Process(Object connectionInfo, Request request)
--- End of inner exception stack trace ---
at Microsoft.SqlServer.Management.Sdk.Sfc.Enumerator.Process(Object connectionInfo, Request request)
at Microsoft.SqlServer.Management.Smo.ExecutionManager.GetEnumeratorData(Request req)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetCollationRelatedProperties(String dbName, ContainmentType& containmentType, CatalogCollationType& catalogCollation)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.GetDbComparer(Boolean inServer)
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.InitializeStringComparer()
at Microsoft.SqlServer.Management.Smo.SqlSmoObject.get_StringComparer()
at Microsoft.SqlServer.Management.Smo.AbstractCollectionBase.get_StringComparer()
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.InitInnerCollection()
at Microsoft.SqlServer.Management.Smo.SmoCollectionBase.GetObjectByKey(ObjectKeyBase key)
at Microsoft.SqlServer.Management.Smo.SimpleObjectCollectionBase.Contains(String name)
at Application.clsDatabaseManagement.GetDbStatus(String connectionString) in C:\Application\DatabaseManagement\DatabaseManagement.cs:line 1404
The solution was to adjust the binding redirect to explicitly use v16 (2022) of the SMO library like so;