如何:設定發行集來允許 Web 同步處理 (RMO 程式設計)
本主題中的程序,是設定合併式複寫的 Web 同步處理時所採取的第一個步驟。如需組態程序的概觀,請參閱<如何:為合併式複寫設定 Web 同步處理 (RMO 程式設計)>。完成本主題中的程序之後,請繼續執行第二個步驟,即設定 IIS 伺服器。第二個步驟會在<如何:設定 Web 同步處理的 IIS>中描述。
本主題將描述 Web 同步處理所需的參數。如需有關如何建立發行集的詳細資訊,請參閱<如何:建立發行集 (RMO 程式設計)>。
將合併式發行集設定為允許 Web 同步處理
使用 ServerConnection 類別建立與發行者的連接。
為發行集資料庫建立 ReplicationDatabase 類別的執行個體。
將 ConnectionContext 屬性設定為步驟 1 中 ServerConnection 的執行個體。
呼叫 LoadProperties 方法。如果此方法傳回 false,請確認此資料庫存在。
如果 EnabledMergePublishing 屬性為 false,請將此屬性設定為 true,然後呼叫 CommitPropertyChanges。
建立 MergePublication 類別的執行個體,然後為此物件設定下列屬性:
將 ConnectionContext 設定為步驟 1 中的 ServerConnection。
將 DatabaseName 設為發行的資料庫名稱。
將 Name 設定為發行集名稱。
將 AllowWebSynchronization 和 AllowPull 的值加入到 Attributes,其方式是使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or) 來啟用 Web 同步處理。
(選擇性) 如果訂閱者將只透過 HTTP 連接到發行者,請使用包含的邏輯 OR 運算子 (Visual C# 中的 | 和 Visual Basic 中的 Or) 將 AllowAnonymous 值加入到 Attributes。
若要針對新的發行集來提供執行快照集代理程式所使用之 Windows 帳戶的認證,請設定 SnapshotGenerationAgentProcessSecurity 的 Login 和 Password 欄位。當快照集代理程式要連接本機散發者及進行任何遠端連接 (使用 Windows 驗證) 時,也會使用此帳戶。
[!附註]
當發行集是由 sysadmin 固定伺服器角色的成員所建立時,不需要設定 SnapshotGenerationAgentProcessSecurity。如需詳細資訊,請參閱<複寫代理程式安全性模型>。
呼叫下列其中一個方法:
若要針對新的發行集來建立已啟用 Web 同步處理的發行集,請呼叫 Create。
若要針對現有的發行集來啟用 Web 同步處理,請呼叫 CommitPropertyChanges。
範例
下列範例會建立啟用 Web 同步處理的發行集。
// Set the Publisher, publication database, and publication names.
string publisherName = publisherInstance;
string publicationName = "AdvWorksSalesOrdersMerge";
string publicationDbName = "AdventureWorks";
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 = "AdventureWorks"
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