public async Task <ChangeFeedProcessor> RunChangeFeedHostAsync()
        {
            string hostName = Guid.NewGuid().ToString();

            Trace.TraceInformation("Host name {0}", hostName);

            var docTransformer = new DefaultDocumentTransformer();

            if (!String.IsNullOrEmpty(config.BlobConnectionString))
            {
                BlobServiceClient blobServiceClient = new BlobServiceClient(config.BlobConnectionString);
                containerClient = blobServiceClient.GetBlobContainerClient(config.BlobContainerName);
                await containerClient.CreateIfNotExistsAsync();
            }

            DateTime starttime = DateTime.MinValue.ToUniversalTime();

            if (config.DataAgeInHours.HasValue)
            {
                if (config.DataAgeInHours.Value >= 0)
                {
                    starttime = DateTime.UtcNow.Subtract(TimeSpan.FromHours(config.DataAgeInHours.Value));
                }
            }

            changeFeedProcessor = sourceCollectionClient.GetContainer(config.MonitoredDbName, config.MonitoredCollectionName)
                                  .GetChangeFeedProcessorBuilder <Document>("Live Data Migrator", ProcessChangesAsync)
                                  .WithInstanceName(hostName)
                                  .WithLeaseContainer(leaseCollectionClient.GetContainer(config.LeaseDbName, config.LeaseCollectionName))
                                  .WithLeaseConfiguration(TimeSpan.FromSeconds(30))
                                  .WithStartTime(starttime)
                                  .WithMaxItems(1000)
                                  .Build();

            await changeFeedProcessor.StartAsync().ConfigureAwait(false);

            return(changeFeedProcessor);
        }
        public async Task <IChangeFeedProcessor> RunChangeFeedHostAsync()
        {
            string hostName = Guid.NewGuid().ToString();

            Trace.TraceInformation("Host name {0}", hostName);

            // monitored collection info
            var documentCollectionLocation = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.config.MonitoredUri),
                MasterKey      = this.config.MonitoredSecretKey,
                DatabaseName   = this.config.MonitoredDbName,
                CollectionName = this.config.MonitoredCollectionName
            };

            var policy = new ConnectionPolicy()
            {
                ConnectionMode     = ConnectionMode.Direct,
                ConnectionProtocol = Protocol.Tcp
            };

            policy.PreferredLocations.Add("North Europe");

            // lease collection info
            var leaseCollectionLocation = new DocumentCollectionInfo
            {
                Uri              = new Uri(this.config.LeaseUri),
                MasterKey        = this.config.LeaseSecretKey,
                DatabaseName     = this.config.LeaseDbName,
                CollectionName   = this.config.LeaseCollectionName,
                ConnectionPolicy = policy
            };

            // destination collection info
            var destCollInfo = new DocumentCollectionInfo
            {
                Uri            = new Uri(this.config.DestUri),
                MasterKey      = this.config.DestSecretKey,
                DatabaseName   = this.config.DestDbName,
                CollectionName = this.config.DestCollectionName
            };

            var processorOptions = new ChangeFeedProcessorOptions();

            if (config.DataAgeInHours.HasValue)
            {
                if (config.DataAgeInHours.Value >= 0)
                {
                    processorOptions.StartTime = DateTime.UtcNow.Subtract(TimeSpan.FromHours(config.DataAgeInHours.Value));
                }
            }
            else
            {
                processorOptions.StartFromBeginning = true;
            }

            processorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(30);

            Trace.TraceInformation("Processor options Starts from Beginning - {0}, Lease renew interval - {1}",
                                   processorOptions.StartFromBeginning,
                                   processorOptions.LeaseRenewInterval.ToString());

            processorOptions.MaxItemCount = 1000;
            var destClient = new DocumentClient(destCollInfo.Uri, destCollInfo.MasterKey,
                                                new ConnectionPolicy()
            {
                ConnectionMode = ConnectionMode.Direct, ConnectionProtocol = Protocol.Tcp
            },
                                                ConsistencyLevel.Eventual);

            var docTransformer = new DefaultDocumentTransformer();

            BlobContainerClient containerClient = null;

            if (!String.IsNullOrEmpty(config.BlobConnectionString))
            {
                BlobServiceClient blobServiceClient = new BlobServiceClient(config.BlobConnectionString);
                containerClient = blobServiceClient.GetBlobContainerClient(config.BlobContainerName);
                await containerClient.CreateIfNotExistsAsync();
            }

            var docObserverFactory = new DocumentFeedObserverFactory(config.SourcePartitionKeys, config.TargetPartitionKey, destClient, destCollInfo, docTransformer, containerClient);

            changeFeedProcessor = await new ChangeFeedProcessorBuilder()
                                  .WithObserverFactory(docObserverFactory)
                                  .WithHostName(hostName)
                                  .WithFeedCollection(documentCollectionLocation)
                                  .WithLeaseCollection(leaseCollectionLocation)
                                  .WithProcessorOptions(processorOptions)
                                  .WithFeedDocumentClient(new DocumentClient(documentCollectionLocation.Uri, documentCollectionLocation.MasterKey, policy, ConsistencyLevel.Eventual))
                                  .BuildAsync();
            await changeFeedProcessor.StartAsync().ConfigureAwait(false);

            return(changeFeedProcessor);
        }