How to: Configure Publishing and Distribution (RMO Programming)

You can programmatically configure replication publishing and distribution by using Replication Management Objects (RMO).

To configure publishing and distribution on a single server

  1. Create a connection to the server by using the ServerConnection class.

  2. Create an instance of the ReplicationServer class. Pass the ServerConnection from step 1.

  3. Create an instance of the DistributionDatabase class.

  4. Set the Name property to the database name and set the ConnectionContext property to the ServerConnection from step 1.

  5. Install the Distributor by calling the InstallDistributor method. Pass the DistributionDatabase object from step 3.

  6. Create an instance of the DistributionPublisher class.

  7. Set the following properties of DistributionPublisher:

  8. Call the Create method.

To configure publishing and distribution using a remote Distributor

  1. Create a connection to the remote Distributor server by using the ServerConnection class.

  2. Create an instance of the ReplicationServer class. Pass the ServerConnection from step 1.

  3. Create an instance of the DistributionDatabase class.

  4. Set the Name property to the database name, and set the ConnectionContext property to the ServerConnection from step 1.

  5. Install the Distributor by calling the InstallDistributor method. Specify a secure password (used by the Publisher when connecting to the remote Distributor) and the DistributionDatabase object from step 3. For more information, see Securing the Distributor.

    Security noteSecurity Note

    When possible, prompt users to enter security credentials at runtime. If you must store credentials, use the cryptographic services provided by the Microsoft Windows .NET Framework.

  6. Create an instance of the DistributionPublisher class.

  7. Set the following properties of DistributionPublisher:

  8. Call the Create method.

  9. Create a connection to the local Publisher server by using the ServerConnection class.

  10. Create an instance of the ReplicationServer class. Pass the ServerConnection from step 9.

  11. Call the InstallDistributor method. Pass the name of the remote Distributor and the password for the remote Distributor specified in step 5.

    Security noteSecurity Note

    When possible, prompt users to enter security credentials at runtime. If you must store credentials, use the cryptographic services provided by the Windows .NET Framework.

Example

The following example configures a server as a Publisher with a local Distributor.

         // Set the server and database names
            string distributionDbName = "distribution";
            string publisherName = publisherInstance;
            string publicationDbName = "AdventureWorks2008R2";

            DistributionDatabase distributionDb;
            ReplicationServer distributor;
            DistributionPublisher publisher;
            ReplicationDatabase publicationDb;

            // Create a connection to the server using Windows Authentication.
            ServerConnection conn = new ServerConnection(publisherName);

            try
            {
                // Connect to the server acting as the Distributor 
                // and local Publisher.
                conn.Connect();

                // Define the distribution database at the Distributor,
                // but do not create it now.
                distributionDb = new DistributionDatabase(distributionDbName, conn);
                distributionDb.MaxDistributionRetention = 96;
                distributionDb.HistoryRetention = 120;

                // Set the Distributor properties and install the Distributor.
                // This also creates the specified distribution database.
                distributor = new ReplicationServer(conn);
                distributor.InstallDistributor((string)null, distributionDb);

                // Set the Publisher properties and install the Publisher.
                publisher = new DistributionPublisher(publisherName, conn);
                publisher.DistributionDatabase = distributionDb.Name;
                publisher.WorkingDirectory = @"\\" + publisherName + @"\repldata";
                publisher.PublisherSecurity.WindowsAuthentication = true;
                publisher.Create();

                // Enable AdventureWorks as a publication database.
                publicationDb = new ReplicationDatabase(publicationDbName, conn);

                publicationDb.EnabledTransPublishing = true;
                publicationDb.EnabledMergePublishing = true;
            }
            catch (Exception ex)
            {
                // Implement appropriate error handling here.
                throw new ApplicationException("An error occured when installing distribution and publishing.", ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Set the server and database names
Dim distributionDbName As String = "distribution"
Dim publisherName As String = publisherInstance
Dim publicationDbName As String = "AdventureWorks2008R2"

Dim distributionDb As DistributionDatabase
Dim distributor As ReplicationServer
Dim publisher As DistributionPublisher
Dim publicationDb As ReplicationDatabase

' Create a connection to the server using Windows Authentication.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the server acting as the Distributor 
    ' and local Publisher.
    conn.Connect()

    ' Define the distribution database at the Distributor,
    ' but do not create it now.
    distributionDb = New DistributionDatabase(distributionDbName, conn)
    distributionDb.MaxDistributionRetention = 96
    distributionDb.HistoryRetention = 120

    ' Set the Distributor properties and install the Distributor.
    ' This also creates the specified distribution database.
    distributor = New ReplicationServer(conn)
    distributor.InstallDistributor((CType(Nothing, String)), distributionDb)

    ' Set the Publisher properties and install the Publisher.
    publisher = New DistributionPublisher(publisherName, conn)
    publisher.DistributionDatabase = distributionDb.Name
    publisher.WorkingDirectory = "\\" + publisherName + "\repldata"
    publisher.PublisherSecurity.WindowsAuthentication = True
    publisher.Create()

    ' Enable AdventureWorks as a publication database.
    publicationDb = New ReplicationDatabase(publicationDbName, conn)

    publicationDb.EnabledTransPublishing = True
    publicationDb.EnabledMergePublishing = True

Catch ex As Exception
    ' Implement appropriate error handling here.
    Throw New ApplicationException("An error occured when installing distribution and publishing.", ex)

Finally
    conn.Disconnect()

End Try