Vorgehensweise: Konfigurieren eines Abonnements für die Websynchronisierung (RMO-Programmierung)

Das Verfahren in diesem Thema ist der dritte Schritt zur Konfiguration der Websynchronisierung für die Mergereplikation. Sie führen diesen Schritt aus, nachdem Sie die Veröffentlichung aktiviert und den Computer konfiguriert haben, auf dem Microsoft Internetinformationsdienste (Internet Information Services, IIS) ausgeführt werden. Eine Übersicht über den Konfigurationsprozess bietet Vorgehensweise: Konfigurieren der Websynchronisierung für die Mergereplikation (RMO-Programmierung). Wenn Sie ein Abonnement zur Verwendung der Websynchronisierung für Abonnenten konfigurieren, die nur über HTTP eine Verbindung mit dem Verleger herstellen können, müssen Sie die Veröffentlichung ordnungsgemäß konfigurieren. Weitere Informationen finden Sie unter Vorgehensweise: Konfigurieren einer Veröffentlichung für die Websynchronisierung (RMO-Programmierung). Nachdem Sie die Verfahren in diesem Thema durchgeführt haben, synchronisieren Sie das von Ihnen erstellte Abonnement. Weitere Informationen finden Sie unter Vorgehensweise: Synchronisieren eines Pullabonnements (RMO-Programmierung).

In diesem Thema werden die Parameter, die für die Websynchronisierung erforderlich sind, beschrieben. Weitere Informationen zum Erstellen von Pullabonnements finden Sie unter Vorgehensweise: Erstellen eines Pullabonnements (RMO-Programmierung).

Wichtiger HinweisWichtig

Die URL des für die Websynchronisierung verwendeten Webservers (z. B. https://server.domain.com/directory/replisapi.dll) gibt den Speicherort der replisapi.dll an. Wenn der Server für die Verwendung eines Anschlusses konfiguriert ist, der nicht dem Standardanschluss 443 für SSL (Secure Sockets Layer) entspricht, müssen Sie auch den Anschluss angeben: https://server.domain.com:PortNumber/directory/replisapi.dll. Der Name des Servers in der URL muss mit dem Namen identisch sein, der beim Erstellen des Zertifikats verwendet wurde. In einem Intranet beispielsweise können Sie möglicherweise über https://server/ auf einen Webserver zugreifen. Wenn jedoch der vollqualifizierte Name (z. B. https://server.domain.com/) beim Erstellen des Zertifikats verwendet wurde, müssen Sie diesen vollqualifizierten Namen in der URL verwenden (https://server.domain.com/directory/replisapi.dll).

So konfigurieren Sie ein Abonnement zur Verwendung der Websynchronisierung

  1. Erstellen Sie Verbindungen mit dem Abonnenten und dem Verleger, indem Sie die ServerConnection-Klasse verwenden.

  2. Erstellen Sie eine Instanz der MergePublication-Klasse, indem Sie die Verlegerverbindung aus Schritt 1 verwenden.

  3. (Optional) Wenn die Abonnementdatenbank nicht vorhanden ist, erstellen Sie die Datenbank, indem Sie die Database-Klasse der SMO (SQL Server Management Objects) verwenden. Weitere Informationen finden Sie unter Erstellen, Ändern und Entfernen von Datenbanken.

  4. Erstellen Sie eine Instanz der MergePullSubscription-Klasse.

  5. Legen Sie folgende Eigenschaften für das Abonnement fest:

  6. Rufen Sie die Create-Methode auf.

  7. Rufen Sie mit der MergePublication-Instanz aus Schritt 2 die MakePullSubscriptionWellKnown-Methode auf, um das Pullabonnement beim Verleger zu registrieren.

So konfigurieren Sie ein Abonnement zur Verwendung der Websynchronisierung für Abonnenten, die nur mit HTTP über einen Webserver eine Verbindung mit einem Verleger herstellen können

  1. Erstellen Sie eine Verbindung mit dem Abonnenten, indem Sie die ServerConnection-Klasse verwenden.

  2. (Optional) Wenn die Abonnementdatenbank nicht vorhanden ist, erstellen Sie die Datenbank, indem Sie die Database-Klasse der SMO verwenden. Weitere Informationen finden Sie unter Erstellen, Ändern und Entfernen von Datenbanken.

  3. Erstellen Sie eine Instanz der MergePullSubscription-Klasse.

  4. Legen Sie folgende Eigenschaften für das Abonnement fest:

  5. Rufen Sie die Create-Methode auf.

Beispiel

Im folgenden Beispiel wird ein Abonnement erstellt, das mithilfe der Websynchronisierung mit dem Verleger synchronisiert wird.

         // 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

In diesem Beispiel wird ein Abonnement erstellt, das mithilfe der Websynchronisierung für einen Abonnenten, der nur mit HTTP über einen Webserver eine Verbindung mit dem Verleger herstellen kann, mit dem Verleger synchronisiert wird.

// 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