Replication Code Sample

This sample for replication is written in Asp.net C# and will demonstrate how to set up a loop code that will pull all data from replication and how to store LastKey and MaxKey and then use it again for update replication.

Full Replication will set LastKey and MaxKey to 0. Replication will loop and pull data till RecordsRemaining becomes 0. LastKey will include timestamp value that is needed to get next batch of data. At end of replication, LastKey will include PreAction entry value that is needed when Update Replication will be done.

Update Replication uses the LastKey value from previous run, either Full or Update Replication, to get any new data that is available since last run. Replication will loop and pull all new data till RecordsRemaining becomes 0.

Add the following code to Default.aspx.cs

Copy

ASP.Net Code Sample

public partial class _Default : Page
{
    private UCService.ReplRequest request; // Replication Request object 
    private UCService.IUCServiceClient serviceClient;

    protected void Page_Load(object sender, EventArgs e)
    {
        serviceClient = new UCService.IUCServiceClient();
        request = new UCService.ReplRequest();
        request.BatchSize = 100;         // Number of records to get in each Replication call
        request.StoreId = "S0001";       // Store to replicate data for
    }

    protected void butFullReplication_Click(object sender, EventArgs e)
    {
        // Full replication is needed first time Store is activated
        request.FullReplication = true;
        request.LastKey = "0";
        request.MaxKey = "0";
                
        while (true)
        {
            request.LastKey = Session["ItemLKey"] as string; // Set LastKey from previous call
            request.MaxKey = Session["ItemMKey"] as string;  // Set MaxKey from previous call
            UCService.ReplItemResponse result = serviceClient.ReplEcommItems(request);
            Session["ItemLKey"] = result.LastKey;   // Store LastKey
            Session["ItemMKey"] = result.MaxKey;    // Store MaxKey

            foreach (UCService.ReplItem item in result.Items)
            {
                // iterate through the list of returned items
            }
                
            if (result.RecordsRemaining <= 0)
               break;      // Replication is done, no more data
        }
    }

    protected void butUpdateReplication_Click(object sender, EventArgs e)
    {
        // After Full replication, only Update replication is needed from LastKey record point
        request.FullReplication = false;
        while (true)
        {
            request.LastKey = Session["ItemLKey"] as string; // Set LastKey from previous call
            request.MaxKey = Session["ItemMKey"] as string;  // Set MaxKey from previous call
            UCService.ReplItemResponse result = serviceClient.ReplEcommItems(request);
            Session["ItemLKey"] = result.LastKey;   // Store LastKey
            Session["ItemMKey"] = result.MaxKey;    // Store MaxKey

            foreach (UCService.ReplItem item in result.Items)
            {
                // iterate through the list of returned items
            }

            if (result.RecordsRemaining <= 0)
                break;      // Replication is done, no more data
        }
    }
}