Error when programming RMO for SQL 2019: MissingMethodException

Stefan Adriaenssen 66 Reputation points
2020-12-04T09:48:42.133+00:00

Hello,

I'm trying to execute the following lines of C# code:

var connection = new Microsoft.Data.SqlClient.SqlConnection(@"Server=.;Database=master;Integrated Security=true;");
var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(connection);
var replicationServer = new Microsoft.SqlServer.Replication.ReplicationServer(serverConnection);

bool installed = replicationServer.DistributorInstalled;

But this code crashes on the last line, where the DistributorInstalled property is being read. I'm getting a MissingMethodException:

Method not found: 'System.Data.SqlClient.SqlDataReader Microsoft.SqlServer.Management.Common.ServerConnection.ExecuteReader(System.String)'.

I added the following references to this project:

  • NuGet package 'Microsoft.SqlServer.SqlManagementObjects' version 161.44091.28 (this is the last version at the time of writing)
  • C:\Program Files (x86)\Microsoft SQL Server\150\SDK\Assemblies\Microsoft.SqlServer.Rmo.dll
  • C:\Program Files\Microsoft SQL Server\150\SDK\Assemblies\Microsoft.SqlServer.Replication.dll

I also tried switching the Microsoft.SqlServer.Rmo and Microsoft.SqlServer.Replication libraries with versions from the GAC but this doesn't help. I keep getting the same error message.

Can anyone provide some help please?

Thanks,

Stefan Adriaenssen

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,361 questions
0 comments No comments
{count} votes

Accepted answer
  1. David Browne - msft 3,771 Reputation points
    2020-12-07T16:03:45.04+00:00

    The Replication libraries installed as part of the SQL Server install need to be paired with an older, compatible version of SMO. The version of SMO you are using has switched from the .NET Framework SqlClient (System.Data.SqlClient) to the newer standalone one (Microsoft.Data.SqlClient).

    So downgrade the SMO NuGet package to 150.18208.0, and change the code to :

        var connection = new System.Data.SqlClient.SqlConnection(@"Server=.;Database=master;Integrated Security=true;");
        var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(connection);
        var replicationServer = new Microsoft.SqlServer.Replication.ReplicationServer(serverConnection);
    
        bool installed = replicationServer.DistributorInstalled;
    
    1 person found this answer helpful.
    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Olaf Helper 43,246 Reputation points
    2020-12-04T13:14:44.327+00:00

    If I pass the connection info as string to the ServerConnection constructor it works for me; no error message.

     var serverConnection = new Microsoft.SqlServer.Management.Common.ServerConnection(".");
     var replicationServer = new Microsoft.SqlServer.Replication.ReplicationServer(serverConnection);
    
     bool installed = replicationServer.DistributorInstalled;
    
    0 comments No comments

  2. Stefan Adriaenssen 66 Reputation points
    2020-12-04T13:26:50.233+00:00

    Hello,
    Thanks for looking into it. No, it doesn't work for me actually, I still get a MissingMethodException. Which version of SQL are you running? I'm at:
    Microsoft SQL Server 2019 (RTM) - 15.0.2000.5 (X64) Sep 24 2019 13:48:23 Copyright (C) 2019 Microsoft Corporation Developer Edition (64-bit) on Windows Server 2019 Standard 10.0 <X64> (Build 17763: ) (Hypervisor)

    Regards,
    Stefan


  3. CathyJi-MSFT 21,136 Reputation points Microsoft Vendor
    2020-12-07T08:08:30.983+00:00

    Hi @Stefan Adriaenssen ,

    I am not familiar with RMO and C#, please check if below blog could help you.

    Programming SQL Server with SQL Server Management Objects Framework

    Check if the suggestion from SQLServerSteve could help you.
    https://stackoverflow.com/questions/59544293/method-not-found-while-restoring-sql-database-c-sharp

    Best regards,
    Cathy

    0 comments No comments

  4. Stefan Adriaenssen 66 Reputation points
    2020-12-08T13:36:52.397+00:00

    Thanks a lot! That fixed the issue!

    One question though; how would I know which version of SMO matches which version of SQL? Is there a page somewhere on MSDN that describes compatibility, or am I to conclude that SMO version '150' matches SQL version '15.0' by naming convention?

    Regards,
    Stefan