/// <summary> /// Registers change feed observer to update changes read on change feed to destination /// collection. Deregisters change feed observer and closes process when enter key is pressed /// </summary> /// <returns>A Task to allow asynchronous execution</returns> public async Task RunChangeFeedHostAsync() { string hostName = Guid.NewGuid().ToString(); // monitored collection info DocumentCollectionInfo documentCollectionLocation = new DocumentCollectionInfo { Uri = new Uri(this.monitoredUri), MasterKey = this.monitoredSecretKey, DatabaseName = this.monitoredDbName, CollectionName = this.monitoredCollectionName }; // lease collection info DocumentCollectionInfo leaseCollectionLocation = new DocumentCollectionInfo { Uri = new Uri(this.leaseUri), MasterKey = this.leaseSecretKey, DatabaseName = this.leaseDbName, CollectionName = this.leaseCollectionName }; // destination collection info DocumentCollectionInfo destCollInfo = new DocumentCollectionInfo { Uri = new Uri(this.destUri), MasterKey = this.destSecretKey, DatabaseName = this.destDbName, CollectionName = this.destCollectionName }; // Customizable change feed option and host options ChangeFeedOptions feedOptions = new ChangeFeedOptions(); // ie customize StartFromBeginning so change feed reads from beginning // can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning feedOptions.StartFromBeginning = true; ChangeFeedHostOptions feedHostOptions = new ChangeFeedHostOptions(); // ie. customizing lease renewal interval to 15 seconds // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay feedHostOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); using (DocumentClient destClient = new DocumentClient(destCollInfo.Uri, destCollInfo.MasterKey)) { DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(destClient, destCollInfo); ChangeFeedEventHost host = new ChangeFeedEventHost(hostName, documentCollectionLocation, leaseCollectionLocation, feedOptions, feedHostOptions); await host.RegisterObserverFactoryAsync(docObserverFactory); Console.WriteLine("Running... Press enter to stop."); Console.ReadLine(); await host.UnregisterObserversAsync(); } }
/// <summary> /// Registers change feed observer to update changes read on change feed to destination /// collection. Deregisters change feed observer and closes process when enter key is pressed /// </summary> /// <returns>A Task to allow asynchronous execution</returns> public async Task RunChangeFeedHostAsync() { string hostName = Guid.NewGuid().ToString(); // monitored collection info DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(this.monitoredUri), MasterKey = this.monitoredSecretKey, DatabaseName = this.monitoredDbName, CollectionName = this.monitoredCollectionName }; DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(this.leaseUri), MasterKey = this.leaseSecretKey, DatabaseName = this.leaseDbName, CollectionName = this.leaseCollectionName }; DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(); ChangeFeedOptions feedOptions = new ChangeFeedOptions(); /* ie customize StartFromBeginning so change feed reads from beginning * can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning */ feedOptions.StartFromBeginning = true; ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions(); // ie. customizing lease renewal interval to 15 seconds // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(); builder .WithHostName(hostName) .WithFeedCollection(documentCollectionInfo) .WithLeaseCollection(leaseCollectionInfo) .WithProcessorOptions(feedProcessorOptions) .WithObserverFactory(new DocumentFeedObserverFactory()); // .WithObserver<DocumentFeedObserver>(); or just pass a observer var result = await builder.BuildAsync(); await result.StartAsync(); Console.Read(); await result.StopAsync(); }
private static async Task RunChangeFeedHostAsync() { // monitored collection info DocumentCollectionInfo documentCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(cosmosSettings.DbUrl), MasterKey = cosmosSettings.AuthorizationKey, DatabaseName = cosmosSettings.DbName, CollectionName = cosmosSettings.CollectionName }; DocumentCollectionInfo leaseCollectionInfo = new DocumentCollectionInfo { Uri = new Uri(cosmosSettings.DbUrl), MasterKey = cosmosSettings.AuthorizationKey, DatabaseName = cosmosSettings.DbName, CollectionName = cosmosSettings.LeaseCollectionName }; DocumentFeedObserverFactory docObserverFactory = new DocumentFeedObserverFactory(); ChangeFeedOptions feedOptions = new ChangeFeedOptions { /* ie customize StartFromBeginning so change feed reads from beginning * can customize MaxItemCount, PartitonKeyRangeId, RequestContinuation, SessionToken and StartFromBeginning */ StartFromBeginning = true }; ChangeFeedProcessorOptions feedProcessorOptions = new ChangeFeedProcessorOptions(); // ie. customizing lease renewal interval to 15 seconds // can customize LeaseRenewInterval, LeaseAcquireInterval, LeaseExpirationInterval, FeedPollDelay feedProcessorOptions.LeaseRenewInterval = TimeSpan.FromSeconds(15); ChangeFeedProcessorBuilder builder = new ChangeFeedProcessorBuilder(); builder .WithHostName(hostName) .WithFeedCollection(documentCollectionInfo) .WithLeaseCollection(leaseCollectionInfo) .WithProcessorOptions(feedProcessorOptions) .WithObserverFactory(new DocumentFeedObserverFactory()); //.WithObserver<DocumentFeedObserver>(); If no factory then just pass an observer var result = await builder.BuildAsync(); await result.StartAsync(); Console.Read(); await result.StopAsync(); }