How to create a replica in a connected environment


Summary The following describes how to create a replica in a connected environment. A connected environment is where both replica geodatabases are connected on the same local area network (LAN) or wide area network (WAN) during replica creation.

Development licensing Deployment licensing
ArcEditor ArcEditor
ArcInfo ArcInfo
Engine Developer Kit Engine Runtime: Geodatabase Update

To use the code in this article, the following namespaces must be referenced via the using (C#) or Imports (VB.NET) statements. It is also necessary to add the corresponding references to the project in order to gain access to these APIs.

Creating a replica

The following steps describe how to create a replica using ArcObjects:
 
  1. Connect to a geodatabase to host the parent replica and connect to a geodatabase to host the child replica. This requires initializing a geodataserver object for each connection. Geodataserver objects allow access to a geodatabase on a LAN or a WAN through ArcGIS Server. To see how to initialize a geodataserver, refer to How to initialize a geodataserver object.
  2. Call the following method by pasting it into an application and passing in the following parameters:
gdsParent
One of the geodataservers initialized in Step 1. This is the geodataserver that hosts the parent replica.
gdsChild
The other geodataserver initialized in Step 1. This is the geodataserver that hosts the child replica.
replicaName
The name of the replica that to be created. This name cannot already be used in either replica geodatabase.
accessType
Specifies whether to create a two-way, one-way, or checkout replica.
featureDataset
The fully qualified name of the feature dataset to replicate (see the following note).
geometry
The replica geometry. Only features that intersect this geometry will be included in the replica. If null is provided, then all features are replicated.
registerOnly
False means that the child does not already have the data. True means that the data already exists in both geodatabases and new replicas should be registered for this data. In typical cases, set this to false.
Although this example can only be used to replicate a feature dataset and its contents, the code can be altered to accept feature classes and tables.

[C#]
// Creates a replica of all data in a feature dataset.
public void CreateFeatureDatasetReplica(IGeoDataServer parentGDS,
  IGeoDataServer childGDS, String replicaName, esriReplicaAccessType accessType,
  String featureDataset, IGeometry geometry, Boolean registerOnly)
{
  try
  {
    // Set and expand the replica datasets.
    IGPReplicaDataset gpReplicaDataset = new GPReplicaDatasetClass();
    gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset;
    gpReplicaDataset.Name = featureDataset;
    IGPReplicaDatasets gpReplicaDatasets = new GPReplicaDatasetsClass();
    gpReplicaDatasets.Add(gpReplicaDataset);
    IGPReplicaDatasets gpReplicaDatasetsExpand =
      parentGDS.ExpandReplicaDatasets(gpReplicaDatasets);

    // Set the replica description.
    IGPReplicaDescription gpReplicaDesc = new GPReplicaDescriptionClass();
    gpReplicaDesc.ReplicaDatasets = gpReplicaDatasetsExpand;
    gpReplicaDesc.ModelType = esriReplicaModelType.esriModelTypeFullGeodatabase;
    gpReplicaDesc.SingleGeneration = (accessType ==
      esriReplicaAccessType.esriReplicaAccessNone);
    gpReplicaDesc.QueryGeometry = geometry;
    gpReplicaDesc.SpatialRelation =
      esriSpatialRelEnum.esriSpatialRelIndexIntersects;

    // Set the replica options.
    IGPReplicaOptions replicaOptions = new GPReplicaOptionsClass();
    replicaOptions.AccessType = accessType;
    replicaOptions.ChildReconcilePolicy =
      esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone;
    replicaOptions.ParentReconcilePolicy =
      esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone;
    replicaOptions.IsChildFirstSender = true;
    replicaOptions.RegisterReplicaOnly = registerOnly;

    // Create the replica.
    IReplicationAgent replicationAgent = new ReplicationAgentClass();
    replicationAgent.CreateReplica("", parentGDS, childGDS, replicaName,
      gpReplicaDesc, replicaOptions);
  }
  catch (COMException comExc)
  {
    throw new Exception(String.Format(
      "Create replica errored: {0}, Error Code: {1}", comExc.Message,
      comExc.ErrorCode), comExc);
  }
  catch (Exception exc)
  {
    throw new Exception(String.Format("Create replica errored: {0}",
      exc.Message), exc);
  }
}

[VB.NET]
' Creates a replica of all data in a feature dataset.

Public Sub CreateFeatureDatasetReplica(ByVal parentGDS As IGeoDataServer, ByVal childGDS As IGeoDataServer, ByVal replicaName As String, ByVal accessType As esriReplicaAccessType, ByVal featureDataset As String, ByVal geometry As IGeometry, ByVal registerOnly As Boolean)
    
    Try
    ' Set and expand the replica datasets.
    Dim gpReplicaDataset As IGPReplicaDataset = New GPReplicaDatasetClass()
    gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset
    gpReplicaDataset.Name = featureDataset
    Dim gpReplicaDatasets As IGPReplicaDatasets = New GPReplicaDatasetsClass()
    gpReplicaDatasets.Add(gpReplicaDataset)
    Dim gpReplicaDatasetsExpand As IGPReplicaDatasets = parentGDS.ExpandReplicaDatasets(gpReplicaDatasets)
    
    ' Set the replica description.
    Dim gpReplicaDesc As IGPReplicaDescription = New GPReplicaDescriptionClass()
    gpReplicaDesc.ReplicaDatasets = gpReplicaDatasetsExpand
    gpReplicaDesc.ModelType = esriReplicaModelType.esriModelTypeFullGeodatabase
    Dim singleGeneration As Boolean
    If accessType = esriReplicaAccessType.esriReplicaAccessNone Then
        singleGeneration = True
    Else
        singleGeneration = False
    End If
    gpReplicaDesc.SingleGeneration = singleGeneration
    gpReplicaDesc.QueryGeometry = geometry
    gpReplicaDesc.SpatialRelation = esriSpatialRelEnum.esriSpatialRelIndexIntersects
    
    ' Set the replica options.
    Dim replicaOptions As IGPReplicaOptions = New GPReplicaOptionsClass()
    replicaOptions.AccessType = accessType
    replicaOptions.ChildReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.ParentReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.IsChildFirstSender = True
    replicaOptions.RegisterReplicaOnly = registerOnly
    
    ' Create the replica.
    Dim replicationAgent As IReplicationAgent = New ReplicationAgentClass()
    replicationAgent.CreateReplica("", parentGDS, childGDS, replicaName, gpReplicaDesc, replicaOptions)
    Catch comExc As COMException
    Throw New Exception(String.Format("Create replica errored: {0}, Error Code: {1}", comExc.Message, comExc.ErrorCode), comExc)
    Catch exc As Exception
    Throw New Exception(String.Format("Create replica errored: {0}", exc.Message), exc)
    End Try
    
End Sub