Partager via


Procédure : configurer un abonnement pour utiliser la synchronisation Web (programmation RMO)

La procédure de cette rubrique constitue la troisième étape de la configuration de la synchronisation Web pour la réplication de fusion. Vous réalisez cette étape après avoir activé la publication et configuré l'ordinateur qui exécute Microsoft Internet Information Services (IIS). Pour obtenir une vue d'ensemble du processus de configuration, consultez Procédure : configurer la synchronisation Web pour la réplication de fusion (programmation RMO). Lorsque vous configurez un abonnement pour utiliser la synchronisation Web pour les Abonnés qui peuvent se connecter uniquement au serveur de publication via le protocole HTTP, vous devez configurer correctement la publication. Pour plus d'informations, consultez Procédure : configurer une publication qui autorise la synchronisation Web (programmation RMO). Une fois les procédures de cette rubrique terminées, synchronisez l'abonnement créé. Pour plus d'informations, consultez Procédure : synchroniser un abonnement par extraction de données (pull) (Programmation RMO).

Cette rubrique décrit les paramètres requis par la synchronisation Web. Pour plus d'informations sur la manière de créer des abonnements par extraction, consultez Procédure : créer un abonnement par extraction de données (pull) (programmation RMO).

Important

L'URL du serveur Web utilisé pour la synchronisation Web (tel que https://serveur.domaine.com/répertoire/replisapi.dll) spécifie l'emplacement de replisapi.dll. Si le serveur est configuré pour utiliser un port autre que le port 443 par défaut pour le protocole SSL (Secure Sockets Layer), vous devez également indiquer le port : https://serveur.domaine.com:NuméroPort/répertoire/replisapi.dll. Le nom du serveur dans l'URL doit être identique au nom utilisé lors de la création du certificat. Par exemple, dans un intranet, vous pourriez accéder à un serveur Web via https://serveur/. Toutefois, si le nom complet (par exemple https://serveur.domaine.com/) a été utilisé lors de la création du certificat, vous devez utiliser ce nom complet dans l'URL (https://serveur.domaine.com/répertoire/replisapi.dll).

Pour configurer un abonnement pour utiliser la synchronisation Web

  1. Créez des connexions à l'Abonné et au serveur de publication en utilisant la classe ServerConnection.

  2. Créez une instance de la classe MergePublication en utilisant la connexion au serveur de publication créée à l'étape 1.

  3. (Facultatif) Si la base de données d'abonnement n'existe pas, créez cette base de données à l'aide de la classe SMO (SQL Server Management Objects) Database. Pour plus d'informations, consultez Création, modification et suppression de bases de données.

  4. Créez une instance de la classe MergePullSubscription.

  5. Définissez les propriétés suivantes des abonnements :

  6. Appelez la méthode Create.

  7. En utilisant l'instance de MergePublication créée à l'étape 2, appelez la méthode MakePullSubscriptionWellKnown pour inscrire l'abonnement par extraction sur le serveur de publication.

Pour configurer un abonnement pour utiliser la synchronisation Web pour les Abonnés qui peuvent se connecter uniquement au serveur de publication via un serveur Web qui utilise le protocole HTTP

  1. Créez une connexion à l'Abonné en utilisant la classe ServerConnection.

  2. (Facultatif) Si la base de données d'abonnement n'existe pas, créez-la en utilisant la classe SMO Database. Pour plus d'informations, consultez Création, modification et suppression de bases de données.

  3. Créez une instance de la classe MergePullSubscription.

  4. Définissez les propriétés suivantes des abonnements :

  5. Appelez la méthode Create.

Exemple

L'exemple suivant crée un abonnement synchronisé avec le serveur de publication à l'aide de la synchronisation 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

Cet exemple crée un abonnement synchronisé avec le serveur de publication à l'aide de la synchronisation Web pour un Abonné qui peut se connecter uniquement au serveur de publication via un serveur Web qui utilise le protocole HTTP.

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