如何配置订阅以使用 Web 同步(RMO 编程)

本主题的过程是为合并复制配置 Web 同步的第三个步骤。 应在启用发布并配置运行 Microsoft Internet 信息服务 (IIS) 的计算机后执行此步骤。 有关配置过程的概述,请参阅如何为合并复制配置 Web 同步(RMO 编程)。 配置订阅以对只能通过 HTTP 连接到发布服务器的订阅服务器使用 Web 同步时,必须相应配置发布。 有关详细信息,请参阅如何配置发布以允许使用 Web 同步(RMO 编程)。 完成本主题中的步骤后,同步已创建的订阅。 有关详细信息,请参阅如何同步请求订阅(RMO 编程)

本主题介绍了 Web 同步所需的参数。 有关如何创建请求订阅的详细信息,请参阅如何创建请求订阅(RMO 编程)

重要说明重要提示

用于 Web 同步的 Web 服务器的 URL(例如 https://server.domain.com/directory/replisapi.dll)指定了 replisapi.dll 的位置。 如果将服务器配置为对安全套接字层 (SSL) 使用除默认端口 443 以外的端口,则还必须提供该端口:https://server.domain.com:PortNumber/directory/replisapi.dll。 URL 中的服务器的名称必须与创建证书时所使用的名称相同。 例如,在 Intranet 上,可能可以通过 https://server/ 访问 Web 服务器。 但是,如果创建证书时使用的是完全限定名称(例如 https://server.domain.com/),则必须在 URL 中使用此完全限定名称 (https://server.domain.com/directory/replisapi.dll)。

配置订阅以使用 Web 同步

  1. 使用 ServerConnection 类创建与订阅服务器和发布服务器的连接。

  2. 使用步骤 1 中的发布服务器连接创建 MergePublication 类的实例。

  3. (可选)如果订阅数据库不存在,则使用 SQL Server 管理对象 (SMO) Database 类创建该数据库。 有关详细信息,请参阅创建、更改和删除数据库

  4. 创建 MergePullSubscription 类的实例。

  5. 设置下列订阅属性:

  6. 调用 Create 方法。

  7. 使用步骤 2 中的 MergePublication 实例,调用 MakePullSubscriptionWellKnown 方法以向发布服务器注册请求订阅。

将订阅配置为对只能通过使用 HTTP 的 Web 服务器连接到发布服务器的订阅服务器使用 Web 同步

  1. 使用 ServerConnection 类创建与订阅服务器的连接。

  2. (可选)如果订阅数据库不存在,则使用 SMO Database 类创建该数据库。 有关详细信息,请参阅创建、更改和删除数据库

  3. 创建 MergePullSubscription 类的实例。

  4. 设置下列订阅属性:

  5. 调用 Create 方法。

示例

下面的示例创建使用 Web 同步与发布服务器同步的订阅。

         // Define the Publisher, publication, and databases.
            string publicationName = "AdvWorksSalesOrdersMerge";
            string publisherName = publisherInstance;
            string subscriberName = subscriberInstance;
            string subscriptionDbName = "AdventureWorksReplica";
            string publicationDbName = "AdventureWorks";
            string hostname = @"adventure-works\garrett1";
            string webSyncUrl = "https://" + publisherInstance + "/WebSync/replisapi.dll";

            //Create connections to the Publisher and Subscriber.
            ServerConnection subscriberConn = new ServerConnection(subscriberName);
            ServerConnection publisherConn = new ServerConnection(publisherName);

            // Create the objects that we need.
            MergePublication publication;
            MergePullSubscription subscription;

            try
            {
                // Connect to the Subscriber.
                subscriberConn.Connect();

                // Ensure that the publication exists and that 
                // it supports pull subscriptions and Web synchronization.
                publication = new MergePublication();
                publication.Name = publicationName;
                publication.DatabaseName = publicationDbName;
                publication.ConnectionContext = publisherConn;

                if (publication.LoadProperties())
                {
                    if ((publication.Attributes & PublicationAttributes.AllowPull) == 0)
                    {
                        publication.Attributes |= PublicationAttributes.AllowPull;
                    }
                    if ((publication.Attributes & PublicationAttributes.AllowWebSynchronization) == 0)
                    {
                        publication.Attributes |= PublicationAttributes.AllowWebSynchronization;
                    }

                    // Define the pull subscription.
                    subscription = new MergePullSubscription();
                    subscription.ConnectionContext = subscriberConn;
                    subscription.PublisherName = publisherName;
                    subscription.PublicationName = publicationName;
                    subscription.PublicationDBName = publicationDbName;
                    subscription.DatabaseName = subscriptionDbName;
                    subscription.HostName = hostname;

                    // Specify the Windows login credentials for the Merge Agent job.
                    subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
                    subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

                    // Enable Web synchronization.
                    subscription.UseWebSynchronization = true;
                    subscription.InternetUrl = webSyncUrl;

                    // Specify the same Windows credentials to use when connecting to the
                    // Web server using HTTPS Basic Authentication.
                    subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication;
                    subscription.InternetLogin = winLogin;
                    subscription.InternetPassword = winPassword;

                    // Ensure that we create a job for this subscription.
                    subscription.CreateSyncAgentByDefault = true;

                    // Create the pull subscription at the Subscriber.
                    subscription.Create();

                    Boolean registered = false;

                    // Verify that the subscription is not already registered.
                    foreach (MergeSubscription existing
                        in publication.EnumSubscriptions())
                    {
                        if (existing.SubscriberName == subscriberName
                            && existing.SubscriptionDBName == subscriptionDbName
                            && existing.SubscriptionType == SubscriptionOption.Pull)
                        {
                            registered = true;
                        }
                    }
                    if (!registered)
                    {
                        // Register the local subscription with the Publisher.
                        publication.MakePullSubscriptionWellKnown(
                            subscriberName, subscriptionDbName,
                            SubscriptionSyncType.Automatic,
                            MergeSubscriberType.Local, 0);
                    }
                }
                else
                {
                    // Do something here if the publication does not exist.
                    throw new ApplicationException(String.Format(
                        "The publication '{0}' does not exist on {1}.",
                        publicationName, publisherName));
                }
            }
            catch (Exception ex)
            {
                // Implement the appropriate error handling here.
                throw new ApplicationException(String.Format(
                    "The subscription to {0} could not be created.", publicationName), ex);
            }
            finally
            {
                subscriberConn.Disconnect();
                publisherConn.Disconnect();
            }
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
Dim hostname As String = "adventure-works\garrett1"
Dim webSyncUrl As String = "https://" + publisherInstance + "/WebSync/replisapi.dll"

'Create connections to the Publisher and Subscriber.
Dim subscriberConn As ServerConnection = New ServerConnection(subscriberName)
Dim publisherConn As ServerConnection = New ServerConnection(publisherName)

' Create the objects that we need.
Dim publication As MergePublication
Dim subscription As MergePullSubscription

Try
    ' Connect to the Subscriber.
    subscriberConn.Connect()

    ' Ensure that the publication exists and that 
    ' it supports pull subscriptions and Web synchronization.
    publication = New MergePublication()
    publication.Name = publicationName
    publication.DatabaseName = publicationDbName
    publication.ConnectionContext = publisherConn

    If publication.LoadProperties() Then
        If (publication.Attributes And PublicationAttributes.AllowPull) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowPull
        End If
        If (publication.Attributes And PublicationAttributes.AllowWebSynchronization) = 0 Then
            publication.Attributes = publication.Attributes _
            Or PublicationAttributes.AllowWebSynchronization
        End If

        ' Define the pull subscription.
        subscription = New MergePullSubscription()
        subscription.ConnectionContext = subscriberConn
        subscription.PublisherName = publisherName
        subscription.PublicationName = publicationName
        subscription.PublicationDBName = publicationDbName
        subscription.DatabaseName = subscriptionDbName
        subscription.HostName = hostname
        subscription.CreateSyncAgentByDefault = True

        ' Specify the Windows login credentials for the Merge Agent job.
        subscription.SynchronizationAgentProcessSecurity.Login = winLogin
        subscription.SynchronizationAgentProcessSecurity.Password = winPassword

        ' Enable Web synchronization.
        subscription.UseWebSynchronization = True
        subscription.InternetUrl = webSyncUrl

        ' Specify the same Windows credentials to use when connecting to the
        ' Web server using HTTPS Basic Authentication.
        subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication
        subscription.InternetLogin = winLogin
        subscription.InternetPassword = winPassword

        ' Create the pull subscription at the Subscriber.
        subscription.Create()

        Dim registered As Boolean = False

        ' Verify that the subscription is not already registered.
        For Each existing As MergeSubscription In _
        publication.EnumSubscriptions()
            If existing.SubscriberName = subscriberName Then
                registered = True
            End If
        Next
        If Not registered Then
            ' Register the local subscription with the Publisher.
            publication.MakePullSubscriptionWellKnown( _
             subscriberName, subscriptionDbName, _
             SubscriptionSyncType.Automatic, _
             MergeSubscriberType.Local, 0)
        End If
    Else
        ' Do something here if the publication does not exist.
        Throw New ApplicationException(String.Format( _
         "The publication '{0}' does not exist on {1}.", _
         publicationName, publisherName))
    End If
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
     "The subscription to {0} could not be created.", publicationName), ex)
Finally
    subscriberConn.Disconnect()
    publisherConn.Disconnect()
End Try

该示例为只能通过使用 HTTP 的 Web 服务器连接到发布服务器的订阅服务器,创建使用 Web 同步与发布服务器同步的订阅。

// The publication must support anonymous Subscribers, pull 
// subscriptions, and Web synchronization.
// Define the Publisher, publication, and databases.
string publicationName = "AdvWorksSalesOrdersMerge";
string publisherName = publisherInstance;
string subscriberName = subscriberInstance;
string subscriptionDbName = "AdventureWorksReplica";
string publicationDbName = "AdventureWorks";
string hostname = @"adventure-works\garrett1";
string webSyncUrl = "https://" + publisherInstance + "/WebSync/replisapi.dll";

//Create the Subscriber connection.
ServerConnection conn = new ServerConnection(subscriberName);

// Create the objects that we need.
MergePullSubscription subscription;

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

    // Define the pull subscription.
    subscription = new MergePullSubscription();
    subscription.ConnectionContext = conn;
    subscription.PublisherName = publisherName;
    subscription.PublicationName = publicationName;
    subscription.PublicationDBName = publicationDbName;
    subscription.DatabaseName = subscriptionDbName;
    subscription.HostName = hostname;

    // Specify an anonymous Subscriber type since we can't 
    // register at the Publisher with a direct connection.
    subscription.SubscriberType = MergeSubscriberType.Anonymous;

    // Specify the Windows login credentials for the Merge Agent job.
    subscription.SynchronizationAgentProcessSecurity.Login = winLogin;
    subscription.SynchronizationAgentProcessSecurity.Password = winPassword;

    // Enable Web synchronization.
    subscription.UseWebSynchronization = true;
    subscription.InternetUrl = webSyncUrl;

    // Specify the same Windows credentials to use when connecting to the
    // Web server using HTTPS Basic Authentication.
    subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication;
    subscription.InternetLogin = winLogin;
    subscription.InternetPassword = winPassword;

    // Ensure that we create a job for this subscription.
    subscription.CreateSyncAgentByDefault = true;

    // Create the pull subscription at the Subscriber.
    subscription.Create();
}
catch (Exception ex)
{
    // Implement the appropriate error handling here.
    throw new ApplicationException(String.Format(
        "The subscription to {0} could not be created.", publicationName), ex);
}
finally
{
    conn.Disconnect();
}
' The publication must support anonymous Subscribers, pull 
' subscriptions, and Web synchronization.
' Define the Publisher, publication, and databases.
Dim publicationName As String = "AdvWorksSalesOrdersMerge"
Dim publisherName As String = publisherInstance
Dim subscriberName As String = subscriberInstance
Dim subscriptionDbName As String = "AdventureWorksReplica"
Dim publicationDbName As String = "AdventureWorks"
Dim hostname As String = "adventure-works\\garrett1"
Dim webSyncUrl As String = "https://" + publisherInstance + "/WebSync/replisapi.dll"

'Create the Subscriber connection.
Dim conn As ServerConnection = New ServerConnection(subscriberName)

' Create the objects that we need.
Dim subscription As MergePullSubscription

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

    ' Define the pull subscription.
    subscription = New MergePullSubscription()
    subscription.ConnectionContext = conn
    subscription.PublisherName = publisherName
    subscription.PublicationName = publicationName
    subscription.PublicationDBName = publicationDbName
    subscription.DatabaseName = subscriptionDbName
    subscription.HostName = hostname

    ' Specify an anonymous Subscriber type since we can't 
    ' register at the Publisher with a direct connection.
    subscription.SubscriberType = MergeSubscriberType.Anonymous

    ' Specify the Windows login credentials for the Merge Agent job.
    subscription.SynchronizationAgentProcessSecurity.Login = winLogin
    subscription.SynchronizationAgentProcessSecurity.Password = winPassword

    ' Enable Web synchronization.
    subscription.UseWebSynchronization = True
    subscription.InternetUrl = webSyncUrl

    ' Specify the same Windows credentials to use when connecting to the
    ' Web server using HTTPS Basic Authentication.
    subscription.InternetSecurityMode = AuthenticationMethod.BasicAuthentication
    subscription.InternetLogin = winLogin
    subscription.InternetPassword = winPassword

    ' Ensure that we create a job for this subscription.
    subscription.CreateSyncAgentByDefault = True

    ' Create the pull subscription at the Subscriber.
    subscription.Create()
Catch ex As Exception
    ' Implement the appropriate error handling here.
    Throw New ApplicationException(String.Format( _
         "The subscription to {0} could not be created.", publicationName), ex)
Finally
    conn.Disconnect()
End Try