Vorgehensweise: Konfigurieren einer Veröffentlichung für die Websynchronisierung (RMO-Programmierung)

Das Verfahren in diesem Thema ist der erste Schritt zur Konfiguration der Websynchronisierung für die Mergereplikation. Eine Übersicht über den Konfigurationsprozess bietet Vorgehensweise: Konfigurieren der Websynchronisierung für die Mergereplikation (RMO-Programmierung). Nachdem Sie die Verfahren in diesem Thema ausgeführt haben, fahren Sie mit dem zweiten Schritt fort, bei dem der IIS-Server (Internet Information Services) konfiguriert wird. Dieser zweite Schritt wird in Vorgehensweise: Konfigurieren von IIS für die Websynchronisierung beschrieben.

In diesem Thema werden die Parameter beschrieben, die für die Websynchronisierung erforderlich sind. Weitere Informationen dazu, wie Veröffentlichungen erstellt werden, finden Sie unter Vorgehensweise: Erstellen einer Veröffentlichung (RMO-Programmierung).

So konfigurieren Sie eine Mergeveröffentlichung für die Websynchronisierung

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

  2. Erstellen Sie eine Instanz der ReplicationDatabase-Klasse für die Veröffentlichungsdatenbank.

  3. Legen Sie die ConnectionContext-Eigenschaft auf die Instanz von ServerConnection aus Schritt 1 fest.

  4. Rufen Sie die LoadProperties-Methode auf. Wenn diese Methode false zurückgibt, überzeugen Sie sich davon, dass die Datenbank vorhanden ist.

  5. Wenn die EnabledMergePublishing-Eigenschaft den Wert false hat, legen Sie diese Eigenschaft auf true fest, und rufen Sie dann CommitPropertyChanges auf.

  6. Erstellen Sie eine Instanz der MergePublication-Klasse, und legen Sie dann die folgenden Eigenschaften für dieses Objekt fest:

    • Die ServerConnection aus Schritt 1 für ConnectionContext.

    • Den Namen der veröffentlichten Datenbank für DatabaseName.

    • Einen Namen für die Veröffentlichung für Name.

    • Fügen Sie die Werte AllowWebSynchronization und AllowPull zum Wert Attributes hinzu, indem Sie den inklusiven logischen OR-Operator (| in Visual C# und Or in Visual Basic) verwenden, um die Websynchronisierung zu aktivieren.

    • (Optional) Wenn Abonnenten nur eine Verbindung über HTTP mit dem Verleger herstellen, fügen Sie den Wert AllowAnonymous zum Wert Attributes hinzu, indem Sie den inklusiven logischen OR-Operator (| in Visual C# und Or in Visual Basic) verwenden.

    • Um bei einer neuen Veröffentlichung die Anmeldeinformationen für das Windows-Konto bereitzustellen, unter dem der Snapshot-Agent ausgeführt wird, legen Sie die Felder Login und Password von SnapshotGenerationAgentProcessSecurity fest. Dieses Konto wird auch verwendet, wenn der Snapshot-Agent Verbindungen mit dem lokalen Verteiler herstellt, sowie für alle Remoteverbindungen mithilfe der Windows-Authentifizierung.

      HinweisHinweis

      Wenn die Veröffentlichung von einem Mitglied der festen Serverrolle sysadmin erstellt wird, müssen Sie SnapshotGenerationAgentProcessSecurity nicht festzulegen. Weitere Informationen finden Sie unter Sicherheitsmodell des Replikations-Agents.

  7. Rufen Sie eine der folgenden Methoden auf:

    • Um bei einer neuen Veröffentlichung diese Veröffentlichung für die Websynchronisierung zu aktivieren, rufen Sie Create auf.

    • Um bei einer vorhandenen Veröffentlichung diese Veröffentlichung für die Websynchronisierung zu aktivieren, rufen Sie CommitPropertyChanges auf.

Beispiel

Im folgenden Beispiel wird eine Veröffentlichung erstellt, die für Websynchronisierung aktiviert ist.

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