如何:設定發行集來允許 Web 同步處理 (RMO 程式設計)

本主題中的程序,是設定合併式複寫的 Web 同步處理時所採取的第一個步驟。如需組態程序的概觀,請參閱<如何:為合併式複寫設定 Web 同步處理 (RMO 程式設計)>。完成本主題中的程序之後,請繼續執行第二個步驟,即設定 IIS 伺服器。第二個步驟會在<如何:設定 Web 同步處理的 IIS>中描述。

本主題將描述 Web 同步處理所需的參數。如需有關如何建立發行集的詳細資訊,請參閱<如何:建立發行集 (RMO 程式設計)>。

將合併式發行集設定為允許 Web 同步處理

  1. 使用 ServerConnection 類別建立與發行者的連接。

  2. 為發行集資料庫建立 ReplicationDatabase 類別的執行個體。

  3. ConnectionContext 屬性設定為步驟 1 中 ServerConnection 的執行個體。

  4. 呼叫 LoadProperties 方法。如果此方法傳回 false,請確認此資料庫存在。

  5. 如果 EnabledMergePublishing 屬性為 false,請將此屬性設定為 true,然後呼叫 CommitPropertyChanges

  6. 建立 MergePublication 類別的執行個體,然後為此物件設定下列屬性:

  7. 呼叫下列其中一個方法:

    • 若要針對新的發行集來建立已啟用 Web 同步處理的發行集,請呼叫 Create

    • 若要針對現有的發行集來啟用 Web 同步處理,請呼叫 CommitPropertyChanges

範例

下列範例會建立啟用 Web 同步處理的發行集。

          // Set the Publisher, publication database, and publication names.
            string publisherName = publisherInstance;
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publicationDbName = "AdventureWorks2008R2";

            ReplicationDatabase publicationDb;
            MergePublication publication;

            // Create a connection to the Publisher.
            ServerConnection conn = new ServerConnection(publisherName);

            try
            {
                // Connect to the Publisher.
                conn.Connect();

                // Enable the database for merge publication.               
                publicationDb = new ReplicationDatabase(publicationDbName, conn);
                if (publicationDb.LoadProperties())
                {
                    if (!publicationDb.EnabledMergePublishing)
                    {
                        publicationDb.EnabledMergePublishing = true;
                    }
                }
                else
                {
                    // Do something here if the database does not exist. 
                    throw new ApplicationException(String.Format(
                        "The {0} database does not exist on {1}.",
                        publicationDb, publisherName));
                }

                // Set the required properties for the merge publication.
                publication = new MergePublication();
                publication.ConnectionContext = conn;
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;

                // Enable Web synchronization, if not already enabled.
                if ((publication.Attributes & PublicationAttributes.AllowWebSynchronization) == 0)
                {
                    publication.Attributes |= PublicationAttributes.AllowWebSynchronization;
                }

                // Enable pull subscriptions, if not already enabled.
                if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)
                {
                    publication.Attributes |= PublicationAttributes.AllowPull;
                }
                
                // Enable Subscriber requested snapshot generation. 
                publication.Attributes |= PublicationAttributes.AllowSubscriberInitiatedSnapshot;

                // Enable anonymous access for Subscribers that cannot make a direct connetion 
                // to the Publisher. 
                publication.Attributes |= PublicationAttributes.AllowAnonymous;

                // Specify the Windows account under which the Snapshot Agent job runs.
                // This account will be used for the local connection to the 
                // Distributor and all agent connections that use Windows Authentication.
                publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin;
                publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword;

                // Explicitly set the security mode for the Publisher connection
                // Windows Authentication (the default).
                publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = true;

                if (!publication.IsExistingObject)
                {
                    // Create the merge publication and the Snapshot Agent job.
                    publication.Create();
                    publication.CreateSnapshotAgent();
                }
                else
                {
                    throw new ApplicationException(String.Format(
                        "The {0} publication already exists.", publicationName));
                }
            }

            catch (Exception ex)
            {
                // Implement custom application error handling here.
                throw new ApplicationException(String.Format(
                    "The publication {0} could not be created.", publicationName), ex);
            }
            finally
            {
                conn.Disconnect();
            }
' Set the Publisher, publication database, and publication names.
Dim publisherName As String = publisherInstance
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publicationDbName As String = "AdventureWorks2008R2"

Dim publicationDb As ReplicationDatabase
Dim publication As MergePublication

' Create a connection to the Publisher.
Dim conn As ServerConnection = New ServerConnection(publisherName)

Try
    ' Connect to the Publisher.
    conn.Connect()

    ' Enable the database for merge publication.                
    publicationDb = New ReplicationDatabase(publicationDbName, conn)
    If publicationDb.LoadProperties() Then
        If Not publicationDb.EnabledMergePublishing Then
            publicationDb.EnabledMergePublishing = True
        End If
    Else
        ' Do something here if the database does not exist. 
        Throw New ApplicationException(String.Format( _
         "The {0} database does not exist on {1}.", _
         publicationDb, publisherName))
    End If

    ' Set the required properties for the merge publication.
    publication = New MergePublication()
    publication.ConnectionContext = conn
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName

    ' Enable Web synchronization, if not already enabled.
    If (publication.Attributes And PublicationAttributes.AllowWebSynchronization) = 0 Then
        publication.Attributes = publication.Attributes _
        Or PublicationAttributes.AllowWebSynchronization
    End If

    ' Enable pull subscriptions, if not already enabled.
    If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then
        publication.Attributes = publication.Attributes _
        Or PublicationAttributes.AllowPull
    End If

    ' Enable Subscriber requested snapshot generation. 
    publication.Attributes = publication.Attributes _
        Or PublicationAttributes.AllowSubscriberInitiatedSnapshot

    ' Enable anonymous access for Subscribers that cannot 
    ' make a direct connetion to the Publisher. 
    publication.Attributes = publication.Attributes _
        Or PublicationAttributes.AllowAnonymous

    ' Specify the Windows account under which the Snapshot Agent job runs.
    ' This account will be used for the local connection to the 
    ' Distributor and all agent connections that use Windows Authentication.
    publication.SnapshotGenerationAgentProcessSecurity.Login = winLogin
    publication.SnapshotGenerationAgentProcessSecurity.Password = winPassword

    ' Explicitly set the security mode for the Publisher connection
    ' Windows Authentication (the default).
    publication.SnapshotGenerationAgentPublisherSecurity.WindowsAuthentication = True

    If Not publication.IsExistingObject Then
        ' Create the merge publication and the Snapshot Agent job.
        publication.Create()
        publication.CreateSnapshotAgent()
    Else
        Throw New ApplicationException(String.Format( _
            "The {0} publication already exists.", publicationName))
    End If
Catch ex As Exception
    ' Implement custom application error handling here.
    Throw New ApplicationException(String.Format( _
        "The publication {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try