次の方法で共有


Web 同期を許可するようにパブリケーションを構成する方法 (RMO プログラミング)

新規 : 2005 年 12 月 5 日

ここでは、マージ レプリケーション用に Web 同期を構成する最初の手順を示します。構成プロセスの概要については、「マージ レプリケーションの Web 同期を構成する方法 (RMO プログラミング)」を参照してください。ここでの手順が完了したら、2 番目の手順に進み、IIS サーバーを構成します。2 番目の手順については「Web 同期用に IIS を構成する方法」で説明します。

ここでは、Web 同期に必要なパラメータについて説明します。パブリケーション作成の詳細については、「パブリケーションを作成する方法 (RMO プログラミング)」を参照してください。

Web 同期を許可するようにマージ パブリケーションを構成するには

  1. ServerConnection クラスを使用して、パブリッシャへの接続を作成します。

  2. パブリケーション データベースに ReplicationDatabase クラスのインスタンスを作成します。

  3. 手順 1. の ServerConnection インスタンスを ConnectionContext プロパティに設定します。

  4. LoadProperties メソッドを呼び出します。このメソッドが false を返す場合、データベースが存在するかどうかを確認してください。

  5. EnabledMergePublishing プロパティが false の場合、このプロパティを true に設定し、CommitPropertyChanges を呼び出します。

  6. MergePublication クラスのインスタンスを作成し、このオブジェクトに次のプロパティを設定します。

    • 手順 1. の ServerConnectionConnectionContext に指定します。
    • パブリッシュするデータベースの名前を DatabaseName に指定します。
    • Name にパブリケーションの名前を指定します。
    • 包括的論理和演算子 (Visual C# では |、Visual Basic では Or) を使用して、AttributesAllowWebSynchronization および AllowPull を追加し、Web 同期を有効にします。
    • (省略可) HTTP を通じてのみサブスクライバがパブリッシャに接続する場合は、包括的論理和演算子 (Visual C# では |、Visual Basic では Or) を使用して、Attributes に値 AllowAnonymous を追加します。
    • 新規パブリケーションの場合は、SnapshotGenerationAgentProcessSecurityLogin フィールドおよび Password フィールドを設定して、スナップショット エージェントの実行に使用される Windows アカウントの資格情報を指定します。このアカウントは、Windows 認証を使用している場合に、スナップショット エージェントがローカル ディストリビュータへの接続や任意のリモート接続を行うときにも使用されます。
      ms345201.note(ja-jp,SQL.90).gifメモ :
      パブリケーションが sysadmin 固定サーバー ロールのメンバで作成されている場合は、SnapshotGenerationAgentProcessSecurity を設定する必要はありません。詳細については、「レプリケーション エージェントのセキュリティ モデル」を参照してください。
  7. 次のいずれかのメソッドを呼び出します。

    • 新規パブリケーションの場合は、Create を呼び出して、Web 同期を有効にしたパブリケーションを作成します。
    • 既存のパブリケーションの場合は、CommitPropertyChanges を呼び出して、Web 同期を有効にします。

使用例

次の例では、Web 同期を有効にしたパブリケーションを作成します。

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

参照

処理手順

パブリケーションを作成する方法 (RMO プログラミング)

概念

マージ レプリケーションの Web 同期を構成する方法 (レプリケーション Transact-SQL プログラミング)

その他の技術情報

マージ レプリケーションの Web 同期

ヘルプおよび情報

SQL Server 2005 の参考資料の入手