如何配置发布以允许使用 Web 同步(RMO 编程)
本主题中的过程是为合并复制配置 Web 同步的第一步。 有关配置过程的概述,请参阅如何为合并复制配置 Web 同步(RMO 编程)。 在完成本主题中的过程后,请继续第二步“配置 IIS 服务器”。 第二步将在如何为 Web 同步配置 IIS 中进行介绍。
本主题介绍了 Web 同步所需的参数。 有关如何创建发布的详细信息,请参阅如何创建发布(RMO 编程)。
配置合并发布以允许使用 Web 同步
使用 ServerConnection 类创建与发布服务器的连接。
为发布数据库创建 ReplicationDatabase 类的实例。
将 ConnectionContext 属性设置为步骤 1 中的 ServerConnection 实例。
调用 LoadProperties 方法。 如果该方法返回 false,请验证数据库是否存在。
如果 EnabledMergePublishing 属性为 false,则将此属性设置为 true,然后调用 CommitPropertyChanges。
创建 MergePublication 类的一个实例,然后为此对象设置以下属性:
将步骤 1 中的 ServerConnection 设置给 ConnectionContext。
将 DatabaseName 设置为已发布的数据库的名称。
将 Name 属性设置为发布的名称。
通过使用逻辑 OR 运算符(在 Visual C# 中为 |,在 Visual Basic 中为 Or)将值 AllowWebSynchronization 和 AllowPull 添加到 Attributes 中以启用 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 = "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