How to create a replica in a disconnected environment


Summary This article demonstrates how to create a replica in a disconnected environment using ArcObjects. A disconnected environment is one in which the parent and child replica geodatabases are not connected by a local area network (LAN) or a wide area network (WAN).

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.

In this topic

 

Exporting the replica workspace document from the parent replica geodatabase

  1. Connect to a geodatabase to host the parent replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow access to a geodatabase on a LAN or WAN through ArcGIS Server. To see how to initialize a GeoDataServer, refer to How to initialize a GeoDataServer object.
  2. Call the following procedure by pasting it into an application and passing in the following parameters:
parentGDS
The GeoDataServer that initialized in Step 1.  This GeoDataServer will host the parent replica.
replicaName
The string name of the replica to be created.
accessType
This parameter specifies the type of replica to create. Three replica types are supported, including checkout replicas, two-way replicas, and one-way replicas. Two-way and one-way replicas require an ArcSDE geodatabase for the child replica. However, checkout replicas allow personal, file, or ArcSDE geodatabases for the child replica. The parent replica must always be hosted by an ArcSDE geodatabase.
featureDataset
This parameter is the name of the feature dataset to replicate. All datasets to be replicated must be registered as versioned on the parent replica's geodatabase. Two-way and one-way replicas also require the datasets to have a GlobalID column and be stored with a high-precision coordinate system.
geometry
This parameter specifies the replica geometry that is applied as a filter where only features intersecting the geometry are replicated. If the filter is set to NULL or nothing, all features are replicated.
outputDirectory
This parameter specifies the output location for the replica workspace document. This location should point to a directory on a local drive. If this folder exists, it will be replaced. If it doesn't exist, it will be created. The replica workspace document created by this function is written in a compressed ZIP file format. This ZIP file will be written to the sOutputDirectory passed to the function.
 

[C#]
public void CreateDisconnectedReplica(IGeoDataServer parentGDS, String
  replicaName, esriReplicaAccessType accessType, String featureDataset,
  IGeometry geometry, String outputDirectory)
{
  try
  {
    // Set and expand the replica datasets.
    IGPReplicaDatasets gpReplicaDatasets = new GPReplicaDatasets();
    IGPReplicaDataset gpReplicaDataset = new GPReplicaDataset();
    gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset;
    gpReplicaDataset.Name = featureDataset;
    gpReplicaDatasets.Add(gpReplicaDataset);
    IGPReplicaDatasets gpReplicaDatasetsExpand =
      parentGDS.ExpandReplicaDatasets(gpReplicaDatasets);

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

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

    // Set the export options.
    IGDSExportOptions gdsExportOptions = new GDSExportOptions();
    gdsExportOptions.ExportFormat = esriGDSExportFormat.esriGDSExportFormatXml;
    gdsExportOptions.Compressed = true;
    gdsExportOptions.BinaryGeometry = false;

    // Create the replica.
    IGDSData gdsData = parentGDS.CreateReplica("", replicaName, gpReplicaDesc,
      replicaOptions, gdsExportOptions,
      esriGDSTransportType.esriGDSTransportTypeUrl);

    // Force deletion of folder and contents if they exist.
    if (Directory.Exists(outputDirectory))
    {
      Directory.Delete(outputDirectory, true);
    }

    // Create the output folder.
    Directory.CreateDirectory(outputDirectory);

    // Get the compressed replica workspace document file from the URL to the local output directory.
    if (gdsData.TransportType == esriGDSTransportType.esriGDSTransportTypeUrl)
    {
      String fileName = System.IO.Path.GetFileName(gdsData.URL);
      String outputFileName = System.IO.Path.Combine(outputDirectory, fileName);
      WebClient webClient = new WebClient();
      webClient.DownloadFile(gdsData.URL, outputFileName);
      webClient.Dispose();
    }
    else
    {
      // The file has been embedded because there is no output directory set on ArcGIS Server.
      Console.WriteLine("Server is not configured with a virtual directory.");
    }
  }
  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]
Public Sub CreateDisconnectedReplica(ByVal parentGDS As IGeoDataServer, ByVal replicaName As String, ByVal accessType As esriReplicaAccessType, ByVal featureDataset As String, ByVal geometry As IGeometry, ByVal outputDirectory As String)
    
    Try
    ' Set and expand the replica datasets.
    Dim gpReplicaDatasets As IGPReplicaDatasets = New GPReplicaDatasets()
    Dim gpReplicaDataset As IGPReplicaDataset = New GPReplicaDataset()
    gpReplicaDataset.DatasetType = esriDatasetType.esriDTFeatureDataset
    gpReplicaDataset.Name = featureDataset
    gpReplicaDatasets.Add(gpReplicaDataset)
    Dim gpReplicaDatasetsExpand As IGPReplicaDatasets = parentGDS.ExpandReplicaDatasets(gpReplicaDatasets)
    
    ' Set the replica description.
    Dim gpReplicaDesc As IGPReplicaDescription = New GPReplicaDescription()
    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
    
    ' Set the replica options.
    Dim replicaOptions As IGPReplicaOptions = New GPReplicaOptions()
    replicaOptions.AccessType = accessType
    replicaOptions.ChildReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.ParentReconcilePolicy = esriReplicaReconcilePolicyType.esriReplicaResolveConflictsNone
    replicaOptions.IsChildFirstSender = True
    
    ' Set the export options.
    Dim gdsExportOptions As IGDSExportOptions = New GDSExportOptions()
    gdsExportOptions.ExportFormat = esriGDSExportFormat.esriGDSExportFormatXml
    gdsExportOptions.Compressed = True
    gdsExportOptions.BinaryGeometry = False
    
    ' Create the replica.
    Dim gdsData As IGDSData = parentGDS.CreateReplica("", replicaName, gpReplicaDesc, replicaOptions, gdsExportOptions, esriGDSTransportType.esriGDSTransportTypeUrl)
    
    ' Force deletion of folder and contents if they exist.
    If Directory.Exists(outputDirectory) Then
        Directory.Delete(outputDirectory, True)
    End If
    
    ' Create the output folder.
    Directory.CreateDirectory(outputDirectory)
    
    ' Get the compressed replica workspace document file from the URL to the local output directory.
    If gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeUrl Then
        Dim fileName As String = System.IO.Path.GetFileName(gdsData.URL)
        Dim outputFileName As String = System.IO.Path.Combine(outputDirectory, fileName)
        Dim webClient As WebClient = New WebClient()
        webClient.DownloadFile(gdsData.URL, outputFileName)
        webClient.Dispose()
    Else
        ' The file has been embedded because there is no output directory set on ArcGIS Server.
        Console.WriteLine("Server is not configured with a virtual directory.")
    End If
    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

Importing the replica workspace document into the child replica geodatabase

  1. Connect to a geodatabase to host the child replica. This requires initializing a GeoDataServer object. GeoDataServer objects allow access to a geodatabase on a LAN or WAN through ArcGIS Server. To see how to initialize a GeoDataServer, refer to How to initialize a GeoDataServer object.
  2. Call the following procedure by pasting it into an application and passing in the following parameters:
childGDS
This is the GeoDataServer initialized in Step 1. This GeoDataServer will host the child replica.
inputDirectory
This specifies the input location for the replica workspace document. This location should point to the directory on a local drive that holds the replica workspace document. The function expects that the replica workspace document is written in a compressed format (XML compressed as a ZIP file). See the following note:
 
The ZIP file must have the same name as the XML file contained within it. The function can easily be modified to import uncompressed XML by setting the gdsData.Compressed property to false. The function also assumes that there is only one file in this directory. If there are more than one file in the inputDirectory, only the first file will be read.

[C#]
public void ImportReplica(IGeoDataServer childGDS, String inputDirectory)
{
  try
  {
    // Make sure the input directory exists.
    if (!Directory.Exists(inputDirectory))
    {
      throw new DirectoryNotFoundException(String.Format(
        "Input directory could not be found: {0}", inputDirectory));
    }

    // Get the first file from the input directory.
    DirectoryInfo directoryInfo = new DirectoryInfo(inputDirectory);
    FileInfo[] fileInfoArray = directoryInfo.GetFiles();
    if (fileInfoArray.Length == 0)
    {
      throw new FileNotFoundException("No input files could be found.");
    }
    String inputFile = System.IO.Path.Combine(inputDirectory,
      fileInfoArray[0].Name);

    // Read .xml file into a byte array to pass to the gdsData object.
    FileInfo fileInfo = new FileInfo(inputFile);
    long fileLength = fileInfo.Length;
    byte[] fileBytes = new Byte[fileLength];

    System.IO.FileStream fileStream = File.Open(inputFile, FileMode.Open,
      FileAccess.Read);
    BinaryReader binaryReader = new BinaryReader(fileStream);
    binaryReader.Read(fileBytes, 0, (int)fileLength);
    binaryReader.Close();
    fileStream.Close();

    // Embed the GDSData object.
    IGDSData gdsData = new GDSDataClass();
    gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeEmbedded;
    gdsData.Compressed = true;
    gdsData.set_EmbeddedData(ref fileBytes);

    // Import the replica workspace document to create the replica.
    childGDS.ImportData(gdsData,
      esriGDSImportFormat.esriGDSImportFormatXmlWorkspace);
  }
  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]
Public Sub ImportReplica(ByVal childGDS As IGeoDataServer, ByVal inputDirectory As String)
    
    Try
    ' Make sure the input directory exists.
    If (Not Directory.Exists(inputDirectory)) Then
        Throw New DirectoryNotFoundException(String.Format("Input directory could not be found: {0}", inputDirectory))
    End If
    
    ' Get the first file from the input directory.
    Dim directoryInfo As DirectoryInfo = New DirectoryInfo(inputDirectory)
    Dim fileInfoArray As FileInfo() = directoryInfo.GetFiles()
    If fileInfoArray.Length = 0 Then
        Throw New FileNotFoundException("No input files could be found.")
    End If
    Dim inputFile As String = System.IO.Path.Combine(inputDirectory, fileInfoArray(0).Name)
    
    ' Read .xml file into a byte array to pass to the gdsData object.
    Dim fileInfo As FileInfo = New FileInfo(inputFile)
    Dim fileLength As Long = fileInfo.Length
    Dim fileBytes As Byte() = New Byte(CInt(fileLength - 1)) {}
    Dim fileStream As System.IO.FileStream = File.Open(inputFile, FileMode.Open, FileAccess.Read)
    Dim binaryReader As BinaryReader = New BinaryReader(fileStream)
    binaryReader.Read(fileBytes, 0, CInt(Fix(fileLength)))
    binaryReader.Close()
    fileStream.Close()
    
    ' Embed the GDSData object.
    Dim gdsData As IGDSData = New GDSDataClass()
    gdsData.TransportType = esriGDSTransportType.esriGDSTransportTypeEmbedded
    gdsData.Compressed = True
    gdsData.EmbeddedData = fileBytes
    
    ' Import the replica workspace document to create the replica.
    childGDS.ImportData(gdsData, esriGDSImportFormat.esriGDSImportFormatXmlWorkspace)
    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